for循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
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
|
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
|
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 + " "); } } }
|