Forums Register Login

clone lists or objects

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
Enjoy the full beauty of the english language. Embedded in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 914 times.
Similar Threads
Pass value
clone() used on array and object
Cloneable
Understanding superclass and subclass objects
Cloning in java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:39:07.