cloning with generics

S

scott moss

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
 
T

Tilman Bohn

In message <[email protected]>,
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. ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

generics puzzle 57
Cloning is "bad" 0
Help with Github??? 2
How to sort a CSV file with merge sort JAVA 7
Generics annoyance 18
Generics 12
stupid generics 38
Help on java generics 18

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top