java - Taking a copy of my original List gets updated when it shouldn't -
i have bit of code:
private list<attributes> splitdatabasedonnextbranchtotraverse(branches nextnonleafbranchtosearch, list<attributes> attributes) { list<attributes> newattributelisttoreturn = attributes; (attributes attribute: newattributelisttoreturn){ (branches branches : attribute._branches){ branches.checkrowexists(nextnonleafbranchtosearch.returnallrows()); } } return newattributelisttoreturn; }
where have global list of objects pass method take copy of list. copy list changed , should not same global list passed in. when debug code , check global list matches changes of list within method why? don't want global list change needs stay same! how can stop happening making both lists unique 1 appreciated.
assignment copy value of l1
(which reference) l2
. both refer same object.
creating shallow copy pretty easy though:
list<attributes> newattributelisttoreturn = new arraylist<attributes>(attributes);
(just 1 example.)
===================================== tl;dr
java manipulate objects reference, , object variables references. however, java doesn't pass method arguments reference; passes them value. take badswap()
method example:
public void badswap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; }
when badswap()
returns, variables passed arguments still hold original values. method fail if change arguments type from int
to object
, since java passes object references value well. now, here gets tricky:
public void tricky(point arg1, point arg2) { arg1.x = 100; arg1.y = 100; point temp = arg1; arg1 = arg2; arg2 = temp; } public static void main(string [] args) { point pnt1 = new point(0,0); point pnt2 = new point(0,0); system.out.println("x: " + pnt1.x + " y: " +pnt1.y); system.out.println("x: " + pnt2.x + " y: " +pnt2.y); system.out.println(" "); tricky(pnt1,pnt2); system.out.println("x: " + pnt1.x + " y:" + pnt1.y); system.out.println("x: " + pnt2.x + " y: " +pnt2.y); }
if execute this main()
method, see following output:
x: 0 y: 0 x: 0 y: 0 x: 100 y: 100 x: 0 y: 0
the method alters value of pnt1
, though passed value; however, swap of pnt1
and pnt2
fails! major source of confusion. in the main()
method, pnt1
and pnt2
are nothing more object references. when pass pnt1
and pnt2
to the tricky()
method, java passes references value other parameter. means references passed method actually copies of original references. figure 1 below shows 2 references pointing same object after java passes object method.
java copies , passes the reference by value, not object. thus, method manipulation alter objects, since references point original objects. since references copies, swaps fail. figure 2 illustrates, method references swap, not original references. unfortunately, after method call, left unswapped original references. swap succeed outside of method call, need swap original references, not copies.
o'reilly's java in nutshell by david flanagan (see resources) puts best: "java manipulates objects 'by reference,' passes object references methods 'by value.'" result, cannot write standard swap method swap objects.
Comments
Post a Comment