亲宝软件园·资讯

展开

Java 数组复制clone方法实现详解

人气:0

这篇文章主要介绍了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
45
4444
------------数字类型数组clone--------------
true
true
6666
6666
------------数字类型数组=赋值-----------------

三、结果分析

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

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

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

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

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论