How to copy(memory) array in java?

前端 未结 1 1090
失恋的感觉
失恋的感觉 2021-01-23 08:44

Hi I want to copy an array...and I do not want to use \"clone\" which is slow to copy.. I tried arraycopy and copyOf, but it is not working

for (int i = 0; i <         


        
相关标签:
1条回答
  • 2021-01-23 09:28

    The real issue is that you store references in the array. If you want the objects in the new array to be independent of the objects in the original array, you have to make a deep copy. For that, cities[i].clone() is your friend.

    As to your performance issue, it could well be due to the fact that you copy the array during every iteration of the loop. This is very wasteful; a single copy would suffice.

    0 讨论(0)
提交回复
热议问题