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

后端 未结 12 1672
傲寒
傲寒 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

    Here is a workaround to copy all the objects from one arrayList to another:

     ArrayList myObject = new ArrayList();
    ArrayList myTempObject = new ArrayList();
    
    myObject.addAll(myTempObject.subList(0, myTempObject.size()));
    
    
    

    subList is intended to return a List with a range of data. so you can copy the whole arrayList or part of it.

    提交回复
    热议问题