How do I clone a generic List in Java?

前端 未结 14 2004
轻奢々
轻奢々 2020-11-27 12:38

I have an ArrayList that I\'d like to return a copy of. ArrayList has a clone method which has the following signature:



        
14条回答
  •  佛祖请我去吃肉
    2020-11-27 13:27

    I think this should do the trick using the Collections API:

    Note: the copy method runs in linear time.

    //assume oldList exists and has data in it.
    List newList = new ArrayList();
    Collections.copy(newList, oldList);
    

提交回复
热议问题