aspose file tools*
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 Spring in Action this week in the Spring 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
 
Don't get me started about those stupid light bulbs.
 
subject: Problems with addAll() from ArrayList - same reference...