How do I clone a generic List in Java?

前端 未结 14 2005
轻奢々
轻奢々 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:20

    Why would you want to clone? Creating a new list usually makes more sense.

    List strs;
    ...
    List newStrs = new ArrayList<>(strs);
    

    Job done.

提交回复
热议问题