Java ArrayList copy

前端 未结 8 822
说谎
说谎 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条回答
  •  一生所求
    2020-11-22 15:37

    Yes l1 and l2 will point to the same reference, same object.

    If you want to create a new ArrayList based on the other ArrayList you do this:

    List l1 = new ArrayList();
    l1.add("Hello");
    l1.add("World");
    List l2 = new ArrayList(l1); //A new arrayList.
    l2.add("Everybody");
    

    The result will be l1 will still have 2 elements and l2 will have 3 elements.

提交回复
热议问题