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

后端 未结 12 1677
傲寒
傲寒 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:21

    Straightforward way to make deep copy of original list is to add all element from one list to another list.

    ArrayList originalList = new ArrayList();
    ArrayList duplicateList = new ArrayList();
    
    for(Object o : originalList) {
        duplicateList.add(o);
    }
    
    
    

    Now If you make any changes to originalList it will not impact duplicateList.

    提交回复
    热议问题