cloning with generics

Discussion in 'Java' started by scott moss, Dec 20, 2004.

  1. scott moss

    scott moss Guest

    I need to clone a generic ArrayList. But clone(), of coure, returns
    an object and has not, as far as I can tell been "generified".
    Consequently, I need to cast in the old way -- albeit to a generic
    ArrayList. The relevant bits of code are:


    {ArrayList<ArrayList<BindingVariable>> bindings;
    [snip]
    for (ArrayList<BindingVariable> bindingList : newBindings) {
    ArrayList<BindingVariable> currList =
    (ArrayList<BindingVariable>) bindings.get(bIndex).clone();
    ......

    This gives me the compiler warning about an unchecked cast. Without
    the cast I get an incompatible types error since the uncast clone need
    not be an ArrayList<BindingVariable> The code runs but I would prefer
    a compile time error to a runtime error when messing about with the
    code at some later time.

    This isn't a high priority, but I suspect that there are a lot of us
    coming to terms with generics and a solution might be of some general
    interest. Certainly, I'd welcome advice on this.

    Thanks
    Scott
     
    scott moss, Dec 20, 2004
    #1
    1. Advertising

  2. scott moss

    Tilman Bohn Guest

    In message <>,
    scott moss wrote on 20 Dec 2004 09:39:51 -0800:

    [...]
    > This isn't a high priority, but I suspect that there are a lot of us
    > coming to terms with generics and a solution might be of some general
    > interest.


    Which is why it was most recently answered last week. ;-)

    --
    Cheers, Tilman

    `Boy, life takes a long time to live...' -- Steven Wright
     
    Tilman Bohn, Dec 20, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    Cloning Possible?

    Guest, Nov 10, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    401
    Steve C. Orr [MVP, MCSD]
    Nov 11, 2003
  2. =?Utf-8?B?QVZM?=

    cloning

    =?Utf-8?B?QVZM?=, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    497
    Eliyahu Goldin
    Dec 14, 2004
  3. Joel Leong

    Cloning ASPNET account

    Joel Leong, Jun 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    358
    Scott Allen
    Jun 4, 2005
  4. Juergen Berchtel
    Replies:
    1
    Views:
    6,028
    John C. Bollinger
    May 20, 2005
  5. Soul
    Replies:
    0
    Views:
    532
Loading...

Share This Page