Implementation of clone method for Java array replication

This article mainly introduces the implementation of Java array replication clone method in detail. The example code is introduced in great detail, which has certain reference value for everyone's study or work. Friends in need can refer to it

1、 Source code

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;
}

2、 Operation results

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
------------数字类型数组=赋值-----------------

3、 Result analysis

  1. Clone copy: for the array itself, when its elements are of reference type (except string), what is saved in each element of the array is the reference of the object. Therefore, the copied array is also the reference of the object, so it is a shallow copy for the elements of the array object;

  2. Clone copy: for reference types (except strings), changing the value in one will affect the other value, which is equivalent to "shallow copy";

  3. Clone copy: for value types and strings, changing one of the values will not affect the other value, which is equivalent to "deep copy";

  4. After the complex value of the equal sign, modifying one will affect the other

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>