java - create a list from another list and remove elements from copied list -
suppose have following structure , created list this. if temp.remove(0) won't affect on original list temp.get(0).vars.remove(0) remove elements original list too.
i think new arraylist(top.mids) not doing deep copy how come temp.remove(0) doesn't affect on original list?
//top class init part , adding elements omitted list<mid> temp = new arraylist(top.mids); temp.remove(0); temp.get(0).bots.remove(0); public class top{ list<mid> mids = new arraylist<mid>(); } public class mid{ list<bot> bots = new arraylist<bot>(); } public class bot{ int id; }
yes understanding correct. list newlist = new arraylist(collection); shallow copy. can modify newlist without affecting original collection, each reference same elements if modify element in 1 list, other list's element change.
this called shallow copy. here's visual representation of i've described:

the things @ bottom objects in array.
Comments
Post a Comment