I have a question for copying the Java array.

Asked 2 years ago, Updated 2 years ago, 24 views

While solving the problem yesterday, I wanted to copy array A to array B to system.arraycopy and change only the content value of B.

But changing the value of array B also changed the value of array A, which made it a little difficult. If you copy an array to System.arraycopy, it seems that array B refers to array A.

All I want to do is copy the values of the real array, not the references, so that they don't affect each other if the contents change inside. Is there a method in Java that influences each other and copies an array?

Or is it just a way to put the value one by one using a repeating sentence?

Finally, if there is a method, I want to know how much difference there is between the method and the repetition rate.

Please let me know if there is anything wrong with the above!

java

2022-09-22 12:44

1 Answers

Arraycopy does a shallow copy.

If the element in the array is a reference object, it seems that only the address value will be copied.

In the end, I think I need to put it in one by one while I'm on a tour.


2022-09-22 12:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.