aspose file tools
The moose likes Beginning Java and the fly likes clone lists or objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "clone lists or objects" Watch "clone lists or objects" New topic
Author

clone lists or objects

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I want to copy/clone a list of Objects:


// copy reference
s.setNames(s1.getNames());


how can I copy the value instead of the reference?

Is that right?:

// copy Value instead of reference
s.setNames(new ArrayList<Name>(s1.getNames));

Or is there a other way?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

That is the correct way to make a new ArrayList with the same contents of the list returned by getNames() - so yeah, copying the value. It does not create new Name objects inside the list, those values will still be shared between both lists.


Steve
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: clone lists or objects