Will this be an example of a deep copy?

int[] a = new int[10];
for (int i = 0; i < 10; i++) {
    a[i] = randomFill();//randomFill is a method that generates random numbers
}

int[] b = new int[a.length];
for (int j = 0; j < a.length; j++) {
    b[j] = a[j]
}

int[] c = new int[a.length];
for(int k = 0; k < a.length; k++) {
    c[k] = a[k]
}

- both array b and array c - a deep copy of array a? I need to modify array a, but you want to keep its original values ​​so that I can use it later, and the hint I got was to use a deep copy. I can’t say if my code counts as depth ...

+4
source share
2 answers

ais an array from int(s), it has only a primitive value, so the answer is yes. Change b(or c) will not affect a. But you can use Arrays.copyOf(int[], int), for example

int[] b = Arrays.copyOf(a, a.length);
int[] c = Arrays.copyOf(a, a.length);
+3
source

Deep copy . , , /.

+1

Source: https://habr.com/ru/post/1680532/


All Articles