ArrayList shallow copy iterate or clone()

后端 未结 4 2013
粉色の甜心
粉色の甜心 2020-12-06 16:18

I need a shallow copy of an java ArrayList, should I use clone() or iterate over original list and copy elements in to new arrayList, which is fast

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 16:39

    the question says shallowcopy not deepcopy.Copying directly reference from one arraylist reference to another will also work right.Deep copy includes copy individual element in arraylist.

    ArrayList list=new ArrayList();
    list.add(3);
    ArrayList list1=list; //shallow copy...
    

    Is there any problem in this ??

提交回复
热议问题