How to clone ArrayList and also clone its contents?

后端 未结 21 1889
小鲜肉
小鲜肉 2020-11-21 06:42

How can I clone an ArrayList and also clone its items in Java?

For example I have:

ArrayList dogs = getDogs();
ArrayList

        
21条回答
  •  野的像风
    2020-11-21 07:02

    Easy way by using commons-lang-2.3.jar that library of java to clone list

    link download commons-lang-2.3.jar

    How to use

    oldList.........
    List newList = new ArrayList();
    foreach(YourObject obj : oldList){
       newList.add((YourObject)SerializationUtils.clone(obj));
    }
    

    I hope this one can helpful.

    :D

提交回复
热议问题