How can I clone an ArrayList
and also clone its items in Java?
For example I have:
ArrayList dogs = getDogs();
ArrayList
You will need to clone the ArrayList
by hand (by iterating over it and copying each element to a new ArrayList
), because clone()
will not do it for you. Reason for this is that the objects contained in the ArrayList
may not implement Clonable
themselves.
Edit: ... and that is exactly what Varkhan's code does.