Java 复制数组

在本教程中,我们通过示例介绍了在 Java 中复制一维数组和二维数组的不同方法。

在本教程中,我们通过示例介绍了在 Java 中复制一维数组和二维数组的不同方法。

在 Java 中,我们可以将一个数组复制到另一个数组中。您可以使用多种技术在 Java 中复制数组。

使用赋值运算符复制数组

我们举个例子,

public class Main {
    public static void main(String[] args) {

        int [] numbers = {1, 2, 3, 4, 5, 6};
        int [] positiveNumbers = numbers;    // 复制数组

        for (int number: positiveNumbers) {
            System.out.print(number + ", ");
        }
    }
}

输出

1, 2, 3, 4, 5, 6,

在上面的例子中,我们使用了赋值运算符 (=) 来复制一个名为 numbers 数组到另一个名为 positiveNumbers 的数组。

这种技术是最简单的一种,而且效果也很好。但是,这种技术存在问题。如果我们改变一个数组的元素,另外的数组的对应元素也会改变。例如,

public class Main {
    public static void main(String[] args) {

        int [] numbers = {1, 2, 3, 4, 5, 6};
        int [] positiveNumbers = numbers;    // 复制数组

        // 改变第一个元素的值
        numbers[0] = -1;

        for (int number: positiveNumbers) {
            System.out.print(number + ", ");
        }
    }
}

输出

-1, 2, 3, 4, 5, 6,

在这里,我们可以看到我们改变了 numbers 数组的一个值。当我们打印 positiveNumbers 数组,我们可以看到同样的值也发生了变化。

这是因为两个变量都指向同一个数组对象。这是浅拷贝。

现在,要在复制数组的同时创建新的数组对象,我们需要深拷贝而不是浅拷贝。

使用循环构造复制数组

让我们举个例子:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {

        int [] source = {1, 2, 3, 4, 5, 6};
        int [] destination = new int[6];

        // 遍历赋值
        for (int i = 0; i < source.length; ++i) {
            destination[i] = source[i];
        }

         // 将数组转为字符串并打印
        System.out.println(Arrays.toString(destination));
    }
}

输出

[1, 2, 3, 4, 5, 6]

在上面的示例中,我们使用 for 循环遍历源数组的每个元素。在每次迭代中,我们是将源数组的值复制到目标数组。

这里,源数组和目标数组指的是不同的对象(深拷贝)。因此,如果一个数组的元素发生变化,则另一个数组的对应元素不变。

注意声明,

System.out.println(Arrays.toString(destination));

这里使用 toString() 方法将数组转换为字符串。

使用 arraycopy() 方法复制数组

在 Java 中,System 类包含一个名为 arraycopy() 复制数组的方法。与上述两种方法相比,这种方法是一种更好的复制数组的方法。

arraycopy() 方法允许您将源数组的指定部分复制到目标数组。例如:

arraycopy(Object src, int srcPos,Object dest, int destPos, int length)

这里,

  • src - 要复制的源数组
  • srcPos - 源数组中的起始位置(索引)
  • dest - 目标数组,其中元素将从源数组复制
  • destPos - 目标数组中的起始位置(索引)
  • length - 要复制的元素数量

让我们举个例子:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] n1 = {2, 3, 12, 4, 12, -2};

        int[] n3 = new int[5];

        // 创建 n2 数组和 n1 数组大小相同
        int[] n2 = new int[n1.length];

        // 拷贝 n1 数组元素到 n2
        System.arraycopy(n1, 0, n2, 0, n1.length);
        System.out.println("n2 = " + Arrays.toString(n2));

        // 将 n1 数组的从索引 2 开始的 2 个元素拷贝到 n3 数组的索引 1 开始的位置
        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 开始的 2 个元素拷贝到 n3 数组的索引 1 开始的位置

如您所见,整数类型数组的元素的默认初始值为 0。

使用 copyOfRange() 方法复制数组

我们还可以使用 Java Arrays 类中定义的 copyOfRange() 方法来复制数组。例如,

import java.util.Arrays;

public class ArraysCopy {
    public static void main(String[] args) {

        int[] source = {2, 3, 12, 4, 12, -2};

        // 拷贝整个数组
        int[] destination1 = Arrays.copyOfRange(source, 0, source.length);
        System.out.println("destination1 = " + Arrays.toString(destination1));

        // 拷贝数组从索引 2 到索引 5 (不包含)
        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);

copyOfRange() 方法直接返回一个数组,不需要我们提前创建好。

使用循环复制二维数组

与一维数组类似,我们也可以使用 for 循环复制二维数组。例如,

import java.util.Arrays;

public 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) {

            destination[i] = new int[source[i].length];

            for (int j = 0; j < destination[i].length; ++j) {
                destination[i][j] = source[i][j];
            }
        }

        System.out.println(Arrays.deepToString(destination));

    }
}

输出

[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]

在上面的程序中,注意这一行,

System.out.println(Arrays.deepToString(destination);

在这里,deepToString() 方法用于更好的展示二维数组。

使用 arraycopy() 复制二维数组

为了使上面的代码更简单,我们可以将内部循环替换为 System.arraycopy() 赋值一维数组。例如,

import java.util.Arrays;

public 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) {

             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]]

在这里,我们可以看到通过用 System.arraycopy() 方法替换内 for 循环,我们得到了相同的输出。