How to clone ArrayList and also clone its contents?

后端 未结 21 1927
小鲜肉
小鲜肉 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:06

    I think I found a really easy way to make a deep copy ArrayList. Assuming you want to copy a String ArrayList arrayA.

    ArrayListarrayB = new ArrayList();
    arrayB.addAll(arrayA);
    

    Let me know if it doesn't work for you.

提交回复
热议问题