您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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;    //复制数组

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

输出

1, 2, 3, 4, 5, 6

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

 这种技术是最简单的一种,它同样有效。然而,这种技术有一个问题。如果我们改变一个数组的元素,其他数组的相应元素也会改变。例如,

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数组的一个值。当我们打印positiveEnumbers数组时,可以看到相同的值也发生了更改。

这是因为两个数组都引用相同的数组对象。这是因为浅拷贝。要了解有关浅拷贝的更多信息,请访问。

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

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

        //迭代并将元素从源复制到目标
        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循环来遍历源数组的每个元素。在每次迭代中,我们都将元素从source数组复制到destination数组。

在这里,源和目标数组引用不同的对象(深度复制)。因此,如果一个数组的元素被更改,另一个数组的相应元素也将保持不变。

注意以下语句,

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

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

3.使用arraycopy()方法复制数组

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

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

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

这里,

  • src -您要复制的源数组

  • srcPos -源数组中的起始位置(索引)

  • dest -目标数组,将从源中复制元素

  • destPos -目标数组中的起始位置(索引)

  • length -要复制的元素数

让我们举个实例:

//使用Arrays.toString()方法
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];

        //创建长度为n1的n2数组
        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复制元素
         //将元素复制到n3数组的索引1
        //将复制2个元素
        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数组的两个元素被复制到从n3数组1开始的索引中

正如您看到的,int类型数组元素的默认初始值为0。

4.使用copyOfRange()方法复制数组

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

//使用toString()和copyOfRange()方法
import java.util.Arrays;

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(不包括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);

在这里,我们可以看到我们正在创建destination1数组并同时将源数组复制到它。在调用copyOfRange()方法之前,我们不会创建destination1数组。要了解有关该方法的更多信息,请访问。

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

            //为目标数组的每一行分配空间
            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;

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);
        }
     
        //显示目标数组
        System.out.println(Arrays.deepToString(destination));      
    }
}

输出

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

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


联系我
置顶