zoukankan      html  css  js  c++  java
  • Java 数组复制之clone方法

    一、源码

    public class Test1 {
        public static void main(String[] args) {
            // Student[] arrs = new Student[] { new Student() { id = "22" } }; C# 可以简写法,Java不支持
            Student[] arrs = new Student[1];
            Student st = new Student();
            st.id = 12;
            st.name = "qwe";
            arrs[0] = st;
            Student[] arrss = arrs.clone();
            System.out.println(arrs == arrss);
            System.out.println(arrs.equals(arrss));
            arrss[0].id = 56;
            for (int i = 0; i < arrs.length; i++) {
                System.out.println(arrs[i].id + arrs[i].name);
            }
            for (int i = 0; i < arrss.length; i++) {
                System.out.println(arrss[i].id + arrss[i].name);
            }
            System.out.println("-------------自定义对象类型数组clone-------------");
    
            Student[] arrst1 = new Student[1];
            Student st1 = new Student();
            st1.id = 162;
            st1.name = "qkkj";
            arrst1[0] = st1;
            Student[] arrst2 = arrst1;
            System.out.println(arrst1 == arrst2);
            System.out.println(arrst1.equals(arrst2));
            arrst2[0].id = 996;
            for (int i = 0; i < arrst1.length; i++) {
                System.out.println(arrst1[i].id + arrst1[i].name);
            }
            for (int i = 0; i < arrst2.length; i++) {
                System.out.println(arrst2[i].id + arrst2[i].name);
            }
            System.out.println("-------------自定义对象类型数组= 赋值-------------");
    
            var arr1 = new String[] { "we", "dfeffg" };
            var arr = arr1.clone();
            System.out.println(arr1 == arr);
            System.out.println(arr1.equals(arr));
            arr[1] = "ddddddddddddddd";
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
            }
            for (int i = 0; i < arr1.length; i++) {
                System.out.println(arr1[i]);
            }
            System.out.println("------------字符串数组clone--------------");
    
            var arr11 = new String[] { "we", "dfeffg" };
            var arr111 = arr11;
            System.out.println(arr111 == arr11);
            System.out.println(arr111.equals(arr11));
            arr111[1] = "ddddddddddddddd";
            for (int i = 0; i < arr11.length; i++) {
                System.out.println(arr11[i]);
            }
            for (int i = 0; i < arr111.length; i++) {
                System.out.println(arr111[i]);
            }
            System.out.println("------------字符串数组=赋值--------------");
    
            var arr2 = new int[] { 232, 45 };
            var arr3 = arr2.clone();
            System.out.println(arr2 == arr3);
            System.out.println(arr2.equals(arr3));
            arr3[1] = 4444;
            for (int i = 0; i < arr2.length; i++) {
                System.out.println(arr2[i]);
            }
            for (int i = 0; i < arr3.length; i++) {
                System.out.println(arr3[i]);
            }
            System.out.println("------------数字类型数组clone--------------");
    
            var arr4 = new int[] { 23432, 44455 };
            var arr5 = arr4;
            System.out.println(arr4 == arr5);
            System.out.println(arr4.equals(arr5));
            arr4[1] = 6666;
            for (int i = 0; i < arr4.length; i++) {
                System.out.println(arr4[i]);
            }
            for (int i = 0; i < arr5.length; i++) {
                System.out.println(arr5[i]);
            }
            System.out.println("------------数字类型数组=赋值-----------------");
        }
    }
    
    class Student {
        public int id;
        public String name;
    }

    二、运行结果

    false
    false
    56qwe
    56qwe
    -------------自定义对象类型数组clone-------------
    true
    true
    996qkkj
    996qkkj
    -------------自定义对象类型数组= 赋值-------------
    false
    false
    we
    ddddddddddddddd
    we
    dfeffg
    ------------字符串数组clone--------------
    true
    true
    we
    ddddddddddddddd
    we
    ddddddddddddddd
    ------------字符串数组=赋值--------------
    false
    false
    232
    45
    232
    4444
    ------------数字类型数组clone--------------
    true
    true
    23432
    6666
    23432
    6666
    ------------数字类型数组=赋值-----------------

    三、结果分析

      1.clone复制,对于数组本身而言,它的元素是引用类型(字符串除外)的时候,本来数组每个元素中保存的就是对象的引用,所以,拷贝过来的数组自然而言也是对象的引用,所以对于数组对象元素而言,它又是浅拷贝;

      2.clone复制,对于引用类型(字符串除外),改变其中一个中的值,会影响另外一个值,相当于“浅拷贝”;

      3.clone复制,对于值类型、字符串,改变其中一个中的值,不会影响另外一个值,相当于“深拷贝”;

      4.等号复值后修改一个后会影响另一个

  • 相关阅读:
    [C#/.NET]Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例
    玩转Asp.net MVC 的八个扩展点
    float实例讲解
    C#高性能TCP服务的多种实现方式
    如何把SQLServer数据库从高版本降级到低版本?
    ASP.NET MVC Area使用-将Area设置成独立项目
    如何使用ping和tracert命令测试网站访问速度
    ASP.NET MVC 入门10、Action Filter 与 内置的Filter实现(实例-防盗链)
    MVC Action Filter
    c#中单元测试
  • 原文地址:https://www.cnblogs.com/zhuanjiao/p/11874784.html
Copyright © 2011-2022 走看看