文章目录
在本教程中,您将在示例的帮助下了解可用于在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()方法用于将数组转换为字符串。
3. 使用arraycopy()方法复制数据
在Java中,System类包含一个名为arraycopy()的方法来复制数组。与上述两种方法相比,这是一种更好的复制数组的方法。
arraycopy()方法允许您将源数组的指定部分复制到目标数组。例如:
arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
这里:
- src - 您要复制的源数组
- srcPos - 源数组中的起始位置(索引)
- dest - 目标数组,将从源中复制元素
- destPos - 目标数组中的起始位置(索引)
- length - 要复制的元素数量
让我们举个例子:
// 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) - 将n1数组的2个元素复制到n3数组,n1数组的元素索引从2开始,n3数组的索引从1开始。
如您所见,int类型数组元素的默认初始值为0。
4. 使用copyOfRange()方法复制数组
我们还可以使用Java Arrays类中定义的copyOfRange()方法来复制数组。其语法形式如下:
Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)
其中,srcArray 表示源数组;startIndex 表示开始复制的起始索引,目标数组中将包含起始索引对应的元素,另外,startIndex 必须在 0 到 srcArray.length 之间;endIndex 表示终止索引,目标数组中将不包含终止索引对应的元素,endIndex 必须大于等于 startIndex,可以大于 srcArray.length,如果大于 srcArray.length,则目标数组中使用默认值填充。
例如:
// 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,4,12,-2]
destination2 = [12,4,12]
在上面的示例中,请注意以下行:
int[] destination1 = Arrays.copyOfRange(source, 0, source.length);
在这里,我们可以看到我们正在创建destination1数组并同时将source数组复制到它。在调用copyOfRange()方法之前,我们没有创建destination1数组。
5. 复制二维数组
5.1 使用循环复制二维数组
与一维数组类似,我们也可以使用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()方法用于更好的表示二维数组。
5.2 使用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()方法而获得了相同的输出。
参考文档
[1]Parewa Labs Pvt.Java InputStream Class[EB/OL].https://www.programiz.com/java-programming/copy-arrays,2020-01-01.