java - Receiving the content of one arraylist into another and then empty the first one -


i want receive content of arraylist array1 arraylist array2 , empty content of array1:

    public arraylist array1 = new arraylist();     public arraylist array2 = new arraylist();      /* array1 contains data , array2 empty*/      array2.addall(array1);     array1.clear();     system.out.println(array2); 

the problem have array2 empty [].

when remove line array1.clear();, works great , array2 shows content [a1, a2, a3].

i thought when array2 receives content of array1 can clear array1 without problem. removed both place since it's still in memory?

how can right way?

array1.clear() sets objects contained in arraylist null. not ensure objects contained in arraylist garbage collected. objects have references elsewhere not gced till references removed.

and array1= null sets reference null , not garbage collected till there references , objects contained in gced explained above.

as never know when gc done , hence cannot force gc setting objects null. objects gced , when objects go out of scope.

try using array1= null..

or copying can use clone method

array2= (arraylist<object>)array1.clone(); 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -