for循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) {
int[] arr1 = new int[]{6, 5, 4, 3, 2, 1};
int[] arr2 = new int[arr1.length];
//复制
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
//遍历
for (int j : arr2) {
System.out.print(j + " ");
}
}
}

System.ararycopy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) {
int[] arr1 = new int[]{6, 5, 4, 3, 2, 1};
int[] arr2 = new int[arr1.length];
//复制
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
//遍历
for (int j : arr2) {
System.out.print(j + " ");
}
}
}

clone()

如果数组等长,而且对应copy,使用int[] B= A.clone()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* @author LeDao
* @company
* @create 2021-06-21 12:38
*/
public class Test {

public static void main(String[] args) {
int[] arr1 = new int[]{6, 5, 4, 3, 2, 1};
int[] arr2 = arr1.clone();
//遍历
for (int j : arr2) {
System.out.print(j + " ");
}
}
}