How do I copy the contents of one ArrayList into another?

后端 未结 12 1666
傲寒
傲寒 2020-11-27 10:46

I have some data structures, and I would like to use one as a temporary, and another as not temporary.

ArrayList myObject = new ArrayList

        
      
      
      
12条回答
  •  攒了一身酷
    2020-11-27 11:15

    You can use such trick:

    myObject = new ArrayList(myTempObject);
    
    
    

    or use

    myObject = (ArrayList)myTempObject.clone();
    
    
    

    You can get some information about clone() method here

    But you should remember, that all these ways will give you a copy of your List, not all of its elements. So if you change one of the elements in your copied List, it will also be changed in your original List.

    提交回复
    热议问题