Java 复制数组
Java 复制数组
在本教程中,您将通过示例了解在 Java 中复制数组(一维和二维)的不同方法。
在 Java 中,我们可以将一个数组复制到另一个数组中。您可以使用多种技术在 Java 中复制数组。
1。使用赋值运算符复制数组
举个例子吧,
class Main {
public static void main(String[] args) {
int [] numbers = {1, 2, 3, 4, 5, 6};
int [] positiveNumbers = numbers; // copying arrays
for (int number: positiveNumbers) {
System.out.print(number + ", ");
}
}
}
输出 :
1, 2, 3, 4, 5, 6
在上面的例子中,我们使用了赋值运算符 (=
) 复制一个名为 numbers 的数组 到另一个名为 positiveNumbers 的数组 .
这种技术是最简单的一种,它也很有效。然而,这种技术存在一个问题。如果我们改变一个数组的元素,其他数组的对应元素也会改变。例如,
class Main {
public static void main(String[] args) {
int [] numbers = {1, 2, 3, 4, 5, 6};
int [] positiveNumbers = numbers; // copying arrays
// change value of first array
numbers[0] = -1;
// printing the second array
for (int number: positiveNumbers) {
System.out.print(number + ", ");
}
}
}
输出 :
-1, 2, 3, 4, 5, 6
在这里,我们可以看到我们改变了 numbers 的一个值 大批。当我们打印 positiveNumbers 数组,我们可以看到同样的值也发生了变化。
这是因为两个数组都引用同一个数组对象。这是因为浅拷贝。要详细了解浅拷贝,请访问浅拷贝。
现在,要在复制数组的同时创建新的数组对象,我们需要深拷贝而不是浅拷贝。
2。使用循环构造复制数组
举个例子吧:
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int [] source = {1, 2, 3, 4, 5, 6};
int [] destination = new int[6];
// iterate and copy elements from source to destination
for (int i = 0; i < source.length; ++i) {
destination[i] = source[i];
}
// converting array to string
System.out.println(Arrays.toString(destination));
}
}
输出 :
[1, 2, 3, 4, 5, 6]
在上面的例子中,我们使用了 for
循环遍历源数组的每个元素。在每次迭代中,我们都从 source 复制元素 数组到 destination 数组。
在这里,源数组和目标数组引用不同的对象(深拷贝)。因此,如果一个数组的元素发生变化,另一个数组的对应元素不变。
注意声明,
System.out.println(Arrays.toString(destination));
在这里, toString() 方法用于将数组转换为字符串。要了解更多信息,请访问 toString() 方法(Java 官方文档)。
3.使用 arraycopy() 方法复制数组
在 Java 中,System 类包含一个名为 arraycopy()
的方法 复制数组。这种方法比上面两种复制数组的方法更好。
arraycopy()
方法允许您将源数组的指定部分复制到目标数组。例如,
arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
在这里,
- src - 要复制的源数组
- srcPos - 源数组中的起始位置(索引)
- 目标 - 目标数组,其中元素将从源复制
- destPos - 目标数组中的起始位置(索引)
- 长度 - 要复制的元素数量
举个例子吧:
// To use Arrays.toString() method
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[] n1 = {2, 3, 12, 4, 12, -2};
int[] n3 = new int[5];
// Creating n2 array of having length of n1 array
int[] n2 = new int[n1.length];
// copying entire n1 array to n2
System.arraycopy(n1, 0, n2, 0, n1.length);
System.out.println("n2 = " + Arrays.toString(n2));
// copying elements from index 2 on n1 array
// copying element to index 1 of n3 array
// 2 elements will be copied
System.arraycopy(n1, 2, n3, 1, 2);
System.out.println("n3 = " + Arrays.toString(n3));
}
}
输出 :
n2 = [2, 3, 12, 4, 12, -2] n3 = [0, 12, 4, 0, 0]
在上面的例子中,我们使用了 arraycopy()
方法,
System.arraycopy(n1, 0, n2, 0, n1.length)
- n1 中的所有元素 数组被复制到 n2 数组System.arraycopy(n1, 2, n3, 1, 2)
- 2 n1 的元素 从索引 2 开始的数组 被复制到从 1 开始的索引 n3 的 数组
如您所见, int 的元素的默认初始值 类型数组为0。
4.使用 copyOfRange() 方法复制数组
我们还可以使用 Java Arrays 类中定义的 copyOfRange() 方法来复制数组。例如,
// To use toString() and copyOfRange() method
import java.util.Arrays;
class ArraysCopy {
public static void main(String[] args) {
int[] source = {2, 3, 12, 4, 12, -2};
// copying entire source array to destination
int[] destination1 = Arrays.copyOfRange(source, 0, source.length);
System.out.println("destination1 = " + Arrays.toString(destination1));
// copying from index 2 to 5 (5 is not included)
int[] destination2 = Arrays.copyOfRange(source, 2, 5);
System.out.println("destination2 = " + Arrays.toString(destination2));
}
}
输出
destination1 = [2, 3, 12, 4, 12, -2] destination2 = [12, 4, 12]
在上面的例子中,注意这一行,
int[] destination1 = Arrays.copyOfRange(source, 0, source.length);
在这里,我们可以看到我们正在创建 destination1 数组并复制 source 同时排列到它。我们没有创建 destination1 调用 copyOfRange()
之前的数组 方法。要了解有关该方法的更多信息,请访问 Java copyOfRange。
5.使用循环复制二维数组
类似于一维数组,我们也可以使用for
复制二维数组 环形。例如,
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[][] source = {
{1, 2, 3, 4},
{5, 6},
{0, 2, 42, -4, 5}
};
int[][] destination = new int[source.length][];
for (int i = 0; i < destination.length; ++i) {
// allocating space for each row of destination array
destination[i] = new int[source[i].length];
for (int j = 0; j < destination[i].length; ++j) {
destination[i][j] = source[i][j];
}
}
// displaying destination array
System.out.println(Arrays.deepToString(destination));
}
}
输出 :
[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]
在上面的程序中,注意这一行,
System.out.println(Arrays.deepToString(destination);
在这里,deepToString()
方法用于提供二维数组的更好表示。要了解更多信息,请访问 Java deepToString()。
使用arraycopy()复制二维数组
为了让上面的代码更简单,我们可以用 System.arraycopy()
代替内部循环 就像一维数组的情况一样。例如,
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int[][] source = {
{1, 2, 3, 4},
{5, 6},
{0, 2, 42, -4, 5}
};
int[][] destination = new int[source.length][];
for (int i = 0; i < source.length; ++i) {
// allocating space for each row of destination array
destination[i] = new int[source[i].length];
System.arraycopy(source[i], 0, destination[i], 0, destination[i].length);
}
// displaying destination array
System.out.println(Arrays.deepToString(destination));
}
}
输出 :
[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]
在这里,我们可以看到通过替换内部的 for
得到了相同的输出 使用 arraycopy()
循环 方法。
java