在 Java 中复制二维数组
Waleed
2023年10月12日
每当我们尝试将二维数组的元素复制到另一个数组时,我们通常会将一个原始数组分配给目标数组。为什么这种方法在逻辑上是错误的,我们将首先讨论它。
尽管下面提到的解决方案在逻辑上是错误的,但我们想让你知道为什么这个解决方案不起作用。
// Java Program to copy 2-dimensional array
// create 2D array
int[][] arr1 = {{2, 4, 6}, {8, 10, 12}};
// creating the same array with the same size
int[][] arr2 = new int[arr1.length];
// this code does not copy elements of arr1[ ] to arr2[ ] because arr2[ ] sill refer to the same
// location
arr2 = arr1;
当我们写 arr2=arr1
时,我们正在分配 arr2[ ]
对 arr1[ ]
的引用。因此,更改一个数组将同时反映在复制的数组和原始数组中,因为两个数组都指向同一个位置。
为了证明这一事实,请查看下面描述的代码。
// A Java program to demonstrate assigning an array reference doesn't make any sense
public class copy2DArray {
public static void main(String[] args) {
// Create an array arr1[]
int[][] arr1 = {{2, 4, 5}, {6, 8, 10}};
// Create an array arr2[] with the same size
int[][] arr2 = new int[arr1.length][];
// Doesn't copy elements of arr1 to arr2, but only arr2 starts refering arr1
arr2 = arr1;
// Any changing in the arr2 also reflects in the arr1 because
// both are referin to the same location
arr2[0][0] = 88;
System.out.println("Elements of arr1[] are:");
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
System.out.println(arr1[i][j] + " ");
}
}
System.out.println("\n\nElements of arr2[] are:");
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2.length; j++) {
System.out.println(arr2[i][j] + " ");
}
}
}
}
输出:
Elements of arr1[] are:
88
4
6
8
Elements of arr2[] are:
88
4
6
8
在上面的示例中,我们讨论了开发人员在复制二维数组元素时经常犯的常见错误。现在我们将讨论完成相同任务的正确方法。
在 Java 中,我们可以使用以下方法复制数组元素:
- 迭代数组的所有元素并复制每个元素。
- 通过使用
clone()
方法。 - 通过使用
arraycopy()
方法。
在 Java 中使用循环迭代复制二维数组
用于复制二维数组的循环迭代技术。使用此方法,你将体验到任何目标或原始数组修改都不会影响原始数组。因此,原始数组保持不变。
// A Java program to demonstrate assigning an array reference doesn't make any sense
import java.util.Arrays;
public class copy2DArray {
public static void main(String[] args) {
// Create an array arr1[ ]
int[][] arr1 = {{2, 4, 5}, {6, 8, 10}};
// Create an array arr2[ ] with the same size
int[][] arr2 = new int[arr1.length][];
// Copying elements of arr1[ ] to arr2[ ]
for (int i = 0; i < arr1.length; i++) {
// allocation space to each row of arr2[]
arr2[i] = new int[arr1[i].length];
for (int j = 0; j < arr1[i].length; j++) {
arr2[i][j] = arr1[i][j];
}
}
// Any change in the elements of arr2[ ] will not be reflected in an original array
arr2[0][0] = 90;
System.out.println("Elements of arr1[] are:");
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1[0].length; j++) {
System.out.println(arr1[i][j] + " ");
}
}
System.out.println("\n\nElements of arr2[] are:");
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2[0].length; j++) {
System.out.println(arr2[i][j] + " ");
}
}
}
}
输出:
Elements of arr1[] are:
2
4
5
6
8
10
Elements of arr2[] are:
90
4
5
6
8
10
在 Java 中使用 clone()
方法复制二维数组
在之前的方法中,我们使用循环迭代技术来复制二维数组元素。我们可以使用 clone()
方法完成相同的任务。
// A Java program to demonstrate assigning an array reference doesn't make any sense
import java.util.Arrays;
public class copy2DArray {
public static void main(String[] args) {
// Create an array arr1[]
int[][] arr1 = {{2, 4, 5}, {6, 8, 10}};
// Create an array arr2[] with the same size
int[][] arr2 = new int[arr1.length][];
// Copying elements of arr1[ ] to arr2[ ] using the clone() method
for (int i = 0; i < arr1.length; i++) arr2[i] = arr1[i].clone();
// Any change in the elements of arr2[] will not be reflected in an original array
arr2[0][0] = 90;
System.out.println("Elements of arr1[] are:");
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1[0].length; j++) {
System.out.println(arr1[i][j] + " ");
}
}
System.out.println("\n\nElements of arr2[] are:");
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2[0].length; j++) {
System.out.println(arr2[i][j] + " ");
}
}
}
}
输出:
Elements of arr1[] are:
2
4
5
6
8
10
Elements of arr2[] are:
90
4
5
6
8
10
在 Java 中使用 arraycopy()
方法复制二维数组
同样,我们可以使用 arraycopy()
方法复制二维数组。我们可以复制任何二维数组的元素,而无需使用此方法迭代所有数组元素。要使用此方法,我们需要提供以下参数:
src
:你需要复制的源数组srcPos
:原始数组的起始位置。dest
:目标数组。destPos
:目标数组的起始索引。length
:我们要复制的二维数组元素的总数
例外:
NullPointerException
:如果源或目标数组未定义或不存在。ArrayStoreException
:如果开发人员试图将整数数组类型复制到字符串数组中,这种情况会自动触发此异常。
例子:
package sampleProject;
import java.util.Arrays;
public class Codesample {
public static void main(String[] args) {
// Create an array arr1[]
int[][] arr1 = {{2, 4, 5}, {6, 8, 10}};
// Create an array arr2[] with the same size
int[][] arr2 = new int[arr1.length][];
// Copying elements of arr1[] to arr2[] using the clone() method
for (int i = 0; i < arr1.length; i++) {
arr2[i] = new int[arr1[i].length];
System.arraycopy(arr1[i], 0, arr2[i], 0, arr1[i].length);
}
// Any change in the elements of arr2[] will not be reflected in an original array
arr2[0][0] = 90;
System.out.println("Elements of arr1[] are:");
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1[0].length; j++) {
System.out.println(arr1[i][j] + " ");
}
}
System.out.println("\n\nElements of arr2[] are:");
for (int i = 0; i < arr2.length; i++) {
for (int j = 0; j < arr2[0].length; j++) {
System.out.println(arr2[i][j] + " ");
}
}
}
}
输出:
Elements of arr1[] are:
2
4
5
6
8
10
Elements of arr2[] are:
90
4
5
6
8
10