ArrayList shallow copy iterate or clone()

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

    Instead of iterating manually you can use the copy constructor.

    As for the speed difference between that and using clone():

    1. It doesn't matter
    2. Most likely there is none
    3. Do a benchmark for your specific system configuration and use case

提交回复
热议问题