Java数组拷贝:直接引用,clone,Array.copy,System.arraycopy对比

一、各方法简要介绍
1.clone:
是一个naive方法;
2.System.arraycopy
是一个naive方法
3.Arrays.copy
底层由System.arraycopy实现。
public static int[] copyOf(int[] original, int newLength) {
        int[] copy = new int[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
二、实验结论
1. clone
clone可拷贝多维数组
clone多维数组,是拷贝的每个数组的地址;
clone一维数组,拷贝的是每个数组元素;


​​2. Array.copy
方法只能针对一维数组,拷贝的是每个元素;


3. System.arraycopy
方法只能针对一维数组,拷贝的是每个元素;

三、实验代码及结果

1.实验代码

public class TestCopy {
    public static void main(String[] args) {
        /*总结:
        clone可copy多维数组
        clone多维数组,是拷贝的每个数组的地址;
        clone一维数组,拷贝的是每个数组元素;

        Array.copy只能针对一维数组,拷贝的是每个元素;且底层由System.arraycopy实现

        System.arraycopy只能针对一维数组,拷贝的是每个元素;*/

        System.out.println("\n测试一维基本类型数组\n");

        //测试一维数组
        int[] a = {1,2,3};
        int[] b1 = a;
        int[] b2 = Arrays.copyOf(a, a.length);
        int[] b3 = new int[a.length];
        int[] b4 = a.clone();
        System.arraycopy(a, 0, b3, 0, a.length);
        a[0]=100;

        printEach(b1,"直接引用方式");
        printEach(b2,"Arrays.copy方式");
        printEach(b3,"System.arraycopy()方式");
        printEach(b4,"clone方式");

        System.out.println("\n测试二维基本类型数组\n");
        //测试二维数组
        int[][] c = {{1,2,3},{4,5,6}};
        int[][] d1 = c;
        int[][] d2 = new int[2][3];
        for(int i=0; i<c.length; i++){
            d2[i] = Arrays.copyOf(c[i], c[i].length);
        }
        int[][] d3 = new int[2][3];
        d3 = c.clone();
        c[0][0] = 100;
        printEach2(d1,"直接引用方式");
        printEach2(d2,"Arrays.copy方式");
        printEach2(d3,"clone方式");

        System.out.println("\n测试一维对象数组\n");

        String[] e = {"1","2","3"};
        String[] f1 = Arrays.copyOf(e, e.length);
        String[] f2= e.clone();
        e[0] = "100";
        printEachObject(f1, "Arrays.copy方式");
        printEachObject(f2, "clone方式");

        System.out.println("\n测试二维对象数组\n");

        String[][] g = {{"1","2","3"}, {"1","2","3"}};
        String[][] h1 = new String[2][3];
        for(int i=0; i<c.length; i++){
            h1[i] = Arrays.copyOf(g[i], g[i].length);
        }
        String[][] h2= g.clone();
        g[0][0] = "100";
        printEachObject2(h1, "Arrays.copy方式");
        printEachObject2(h2, "clone方式");

    }

    public static void printEach(int[] a, String tag){
        System.out.println(tag);
        for(int i: a){
            System.out.printf("%d ", i);
        }
        System.out.println();
    }

    public static void printEach2(int[][] a, String tag){
        System.out.println(tag);
        for(int[] t: a){
            for(int i: t){
                System.out.printf("%d ", i);
            }
            System.out.println();
        }
    }

    public static <T> void printEachObject(T[] a, String tag){
        System.out.println(tag);
        for(T i: a){
            System.out.print(i + " ");
        }
        System.out.println();
    }

    public static <T> void printEachObject2(T[][] a, String tag){
        System.out.println(tag);
        for(T[] t: a){
            for(T i: t){
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

}

2.实验结果

输出为:

测试一维基本类型数组

直接引用方式
100 2 3 
Arrays.copy方式
1 2 3 
System.arraycopy()方式
1 2 3 
clone方式
1 2 3 

测试二维基本类型数组

直接引用方式
100 2 3 
4 5 6 
Arrays.copy方式
1 2 3 
4 5 6 
clone方式
100 2 3 
4 5 6 

测试一维对象数组

Arrays.copy方式
1 2 3 
clone方式
1 2 3 

测试二维对象数组

Arrays.copy方式
1 2 3 
1 2 3 
clone方式
100 2 3 
1 2 3 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值