java 基础——二维数组的拷贝

本文深入探讨了Java中数组拷贝的多种方法,包括for循环、clone、System.arraycopy和Arrays.copyOf,对比了基本类型与引用类型拷贝的区别,揭示了深拷贝与浅拷贝的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

for 循环拷贝

基本类型

 public static void main(String[] args) {
        int[][] array1 = {{10,10,10},{10,10,10}};
        int[][] array2 = new int[2][3];

        for(int i = 0;i < array1.length;i++){
            for(int j = 0;j < array1[0].length;j++){
                array2[i][j] = array1[i][j];
            }
        }
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
        array2[0][0] = 30;
        System.out.println("===修改后===");
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
    }

运行结果:

数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[10,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
在这里插入图片描述

引用类型

 public static void main(String[] args) {
        test[][] array1 = {{new test(),new test(),new test()},
                        {new test(),new test(),new test()}};
        test[][] array2 = new test[2][3];
        for(int i = 0;i < array1.length;i++){
            for(int j = 0;j < array1[0].length;j++){
                array2[i][j] = array1[i][j];
            }
        }
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
        array2[0][0].setVal(30);
        System.out.println("===修改后===");
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
    }

运行结果:

数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[30,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
在这里插入图片描述

clone 方式拷贝

基本类型

public static void main(String[] args) {
        int[][] array1 = {{10,10,10},{10,10,10}};
        int[][] array2 = new int[2][3];

        for(int i = 0;i < array1.length;i++){
           array2[i] = array1[i].clone();
        }
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
        array2[0][0] = 30;
        System.out.println("===修改后===");
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
    }

运行结果:

数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[10,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
在这里插入图片描述

引用类型

public static void main(String[] args) {
        test[][] array1 = {{new test(),new test(),new test()},
                {new test(),new test(),new test()}};
        test[][] array2 = new test[2][3];
        for(int i = 0;i < array1.length;i++){
                array2[i] = array1[i].clone();
        }
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
        array2[0][0].setVal(30);
        System.out.println("===修改后===");
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
    }

运行结果:

数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[30,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]

在这里插入图片描述

System.arraycopy()

基本类型

public static void main(String[] args) {
        int[][] array1 = {{10,10,10},{10,10,10}};
        int[][] array2 = new int[2][3];

        for(int i = 0;i < array1.length;i++){
            System.arraycopy(array1[i],0,array2[i],0,array1[i].length);
        }
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
        array2[0][0] = 30;
        System.out.println("===修改后===");
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
    }
}

运行结果:

数组1
[10,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]

引用类型

public static void main(String[] args) {
        test[][] array1 = {{new test(),new test(),new test()},
                {new test(),new test(),new test()}};
        test[][] array2 = new test[2][3];
        for(int i = 0;i < array1.length;i++){
            System.arraycopy(array1[i],0,array2[i],0,array1[i].length);
        }
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
        array2[0][0].setVal(30);
        System.out.println("===修改后===");
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
    }

运行结果:

数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[30,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]

Arrays.copyOf()

基本类型

 public static void main(String[] args) {
        int[][] array1 = {{10,10,10},{10,10,10}};
        int[][] array2 = new int[2][3];

        for(int i = 0;i < array1.length;i++){
            array2[i] = Arrays.copyOf(array1[i],array1[i].length);
        }
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
        array2[0][0] = 30;
        System.out.println("===修改后===");
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
    }

运行结果:

数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[10,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]

引用类型

 public static void main1(String[] args) {
        test[][] array1 = {{new test(),new test(),new test()},
                {new test(),new test(),new test()}};
        test[][] array2 = new test[2][3];
        for(int i = 0;i < array1.length;i++){
            array2[i] = Arrays.copyOf(array1[i],array1[i].length);
        }
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
        array2[0][0].setVal(30);
        System.out.println("===修改后===");
        System.out.println("数组1 ");
        show(array1);
        System.out.println("数组2 ");
        show(array2);
    }

运行结果:

数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[30,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]

总结

从上面几个例子我们可以看出,基本类型的拷贝都是深拷贝,引用类型的拷贝都是浅拷贝,这点和一维数组的拷贝没差。
一维数组的拷贝参考博文:java 基础——一维数组拷贝
关于深拷贝和浅拷贝也请参考上篇博文。

### 动态扩展二维数组的方法 在 Java 中,可以通过多种方式实现二维数组的动态扩展。一种常见的方法是使用 `ArrayList` 类来替代传统的固定大小的二维数组。由于传统二维数组一旦定义其大小便不可更改,因此当需要动态调整大小时,通常会采用以下两种策略之一: #### 方法一:重新分配内存并复制原有数据 如果坚持使用基本类型的二维数组,则可以先创建一个新的更大尺寸的二维数组,并将原数组中的所有元素逐一拷贝至新数组中。 ```java int[][] originalArray = {{1, 2}, {3, 4}}; int rows = originalArray.length; int cols = (rows > 0) ? originalArray[0].length : 0; // 创建新的更大的二维数组 int[][] newArray = new int[rows * 2][cols * 2]; // 将原始数组的内容复制到新数组 for (int i = 0; i < rows; i++) { System.arraycopy(originalArray[i], 0, newArray[i], 0, cols); } ``` 这种方法通过手动操作实现了二维数组的扩容[^5]。 #### 方法二:使用 ArrayList 替代二维数组 另一种更灵活的方式是利用 `ArrayList<ArrayList<Integer>>` 结构代替普通的二维数组。这种方式允许随时添加或删除行和列而无需担心预设容量的问题。 ```java import java.util.ArrayList; public class Main { public static void main(String[] args) { // 初始化一个可变的二维列表结构 ArrayList<ArrayList<Integer>> list = new ArrayList<>(); // 添加一些初始值 for (int i = 0; i < 3; ++i) { ArrayList<Integer> row = new ArrayList<>(); for (int j = 0; j < 4; ++j) { row.add(i * j); // 填充示例数值 } list.add(row); } // 扩展该二维列表 ArrayList<Integer> newRow = new ArrayList<>(); newRow.add(99); newRow.add(100); list.add(newRow); // 输出结果验证 for (ArrayList<Integer> row : list) { System.out.println(row.toString()); } } } ``` 上述代码展示了如何构建以及动态修改一个模拟的二维数组。 ### 初始设置较大的二维数组 除了动态扩展外,在最初设计阶段合理估计所需空间也是一种有效手段。比如直接声明较大规模的二维数组以减少后续频繁调整带来的性能开销。 ```java int maxRows = 100; int maxCols = 100; int[][] largeArray = new int[maxRows][maxCols]; ``` 这样做的好处是可以避免多次执行耗时的操作——即不断重建与迁移旧数据到更新版本的大阵列里去[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值