Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clone lists or objects

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic