I have 2 lists and am a little confused about adding an object into both of them like this:
TestClass e;
List lst1;
LinkedList lst2;
...
lst1.add(e);
lst2.add(e);
Will lst1 and lst2 have copies of e? or the reference to the same object e?
If the first is true, how can the code make copies of e if TestClass is not clonable?
Yes.
Both lists will have a copy of the value contained in
e. (Note though, thatecontains a reference and not an actual object.)This means that
Both lists will have one copy each of the reference to the
TestClassobject whicherefers to at the point of theadd.Changes to the
TestClassobject will be visible from both lists references.Performing
e = nullafter the add will not affect the content of the lists (the lists contain copies, remember? 😉