How to clone ArrayList and also clone its contents?

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

    Here is a solution using a generic template type:

    public static  List copyList(List source) {
        List dest = new ArrayList();
        for (T item : source) { dest.add(item); }
        return dest;
    }
    

提交回复
热议问题