ArrayList shallow copy iterate or clone()

后端 未结 4 2007
粉色の甜心
粉色の甜心 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:50

    Use clone(), or use the copy-constructor.

    The copy-constructor makes additional transformation from the passed collection to array, while the clone() method uses the internal array directly.

    Have in mind that clone() returns Object, so you will have to cast to List.

提交回复
热议问题