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

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 基础——一维数组拷贝
关于深拷贝和浅拷贝也请参考上篇博文。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值