File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "clone lists or objects" Watch "clone lists or objects" New topic

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

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

Joined: Jan 28, 2003
Posts: 4181

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.

I agree. Here's the link:
subject: clone lists or objects
It's not a secret anymore!