Array copy
First understand
the four copy methods of deep copy and shallow copy array:
1. For loop copy
Code example:
import java.util.Arrays;
public class TestDemo{
public static void main(String[] args) {
int[] array1 ={
1,2,3,4,5,6};
int[] array2 =new int [array1.length];
for (int i = 0; i <array1.length ; i++) {
array2[i] = array1[i];
}
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));
}
}
Result:
At this time, modify the value of array2 0 subscript and find that it has no effect on array1;
import java.util.Arrays;
public class TestDemo{
public static void main(String[] args) {
int[] array1 ={
1,2,3,4,5,6};
int[] array2 =new int [array1.length];
for (int i = 0; i <array1.length ; i++) {
array2[i] = array1[i];
}
System.out.println("==============");
array2[0]=99;
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));
}
}
Result:
An array object is created, and all the elements in the original array are copied to the new array. Therefore, modifying the original array will not affect the new array, that is, modify the copied value through a reference, and find that it does not affect the original The value of the object, this copy is a deep copy.
2.copyof copy
Code example:
public static void main(String[] args) {
int[] array1 = {
1,2,3,4,5,6};
System.out.println(Arrays.toString(array1));
int[] array2 = Arrays.copyOf(array1,array1.length);
System.out.println("==============");
array2[0]=99;
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));
}
Result:
Same as the first one: copyOf is a deep copy of the array, that is, another array object is created, and all elements in the original array are copied to the new array. Therefore, modifying the original array will not affect the new array.
3.System.arraycopy copy
import java.util.Arrays;
public class TestDemo {
public static void main(String[] args) {
int[] array1 = {
1, 2, 3, 4, 5, 6};
int[] array2 = new int[array1.length];
System.out.println(Arrays.toString(array1));
System.arraycopy(array1, 0, array2, 0, array1.length);
System.out.println("==============");
array2[0] = 99;
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));
}
}
Result:
The copy of System.arraycopy is also a deep copy.
The fastest copy of the first three copy methods is System.arraycopy copy.
4.clone copy
引用.clone
克隆 这个引用所指向的对象
这个引用所指向的对象就会被克隆(生成一个副本)
Code example;
public static void main(String[] args) {
int[] array1 = {
1, 8, 9, 4, 13, 26};
System.out.println(Arrays.toString(array1));
//array2 指向了刚刚生成的副本
int [] array2 = array1.clone();
System.out.println("==============");
array2[0] = 99;
System.out.println(Arrays.toString(array1));
System.out.println(Arrays.toString(array2));
}
Result:
It can also be seen at this time that modifying the original array will not affect the new array. So it is also a deep copy.
In fact, in essence, these four types belong to shallow copy
. The reason why the above four belong to deep copy is because the simple types are stored in the array just now .
If you put reference types , they are all shallow copies.
Drawing to illustrate shallow copy :
When the reference type is stored in the array, modify the value of an element in array2 and also modify the value in array1. This method is called shallow copy .
How to modify it into a deep copy? When we copy array1, we also copy the value it refers to. At this time, we modify the value of an element in array2, and the value in array1 remains unchanged, as shown in the figure:
to sum up:
These four array copy methods are shallow copy
Several small knowledge points:
1. Copy part of the array;
Code example:
public static void main(String[] args) {
int[] array1 = {
2, 8, 9, 10, 12, 26};
int[] ret = Arrays.copyOfRange(array1,2,5);
System.out.println(Arrays.toString(ret));
}
result:
2. Compare two arrays
Code example:
public static void main(String[] args) {
int[] array1 = {
2, 8, 9, 10, 12, 26};
int[] array2 = {
2, 8, 9, 10, 12, 26, 30,35};
System.out.println(Arrays.equals(array1, array2));
}
result:
3. The array is completely filled
Code example:
public static void main(String[] args) {
int[] array1 =new int[10];
Arrays.fill(array1,6);
System.out.println(Arrays.toString(array1));
}
result:
4. Array partially filled
Code example:
public static void main(String[] args) {
int[] array1 =new int[10];
Arrays.fill(array1,2,6,8);
System.out.println(Arrays.toString(array1));
}
Result:
finished. (Take my life)