This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problems with addAll() from ArrayList - same reference... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems with addAll() from ArrayList - same reference..." Watch "Problems with addAll() from ArrayList - same reference..." New topic
Author

Problems with addAll() from ArrayList - same reference...

Luciano Assis
Greenhorn

Joined: Apr 30, 2008
Posts: 16
Hello folks,

I want to clone an arrayList, but with different references (2 objects).

I tried to create a new arraylist and use de method addAll() passing the list, but it still continue with the same reference, if i modify some propertie of the new arrayList, the same propertie from the original list is modified too..

How can i clone an arraylist with 2 distinct objects?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See the Cloning objects topic from Eckel's Thinking in Java. This section specifically addresses cloning an ArrayList and talks about "shallow" vs. "deep" copies (A deep copy with ArrayList).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problems with addAll() from ArrayList - same reference...
 
Similar Threads
copying arrays from one arraylist to another arraylist
Cloning Doubt
clone lists or objects
Problem with Cloning...
Collection addAll method Strange Behavior