Java ArrayList copy

前端 未结 8 829
说谎
说谎 2020-11-22 15:19

I have an ArrayList l1 of size 10. I assign l1 to new list reference type l2. Will l1 and l2 po

8条回答
  •  Happy的楠姐
    2020-11-22 15:54

    Yes, assignment will just copy the value of l1 (which is a reference) to l2. They will both refer to the same object.

    Creating a shallow copy is pretty easy though:

    List newList = new ArrayList<>(oldList);
    

    (Just as one example.)

提交回复
热议问题