189 8069 5689

Java数组复制clone方法实现详解-创新互联

这篇文章主要介绍了Java 数组复制clone方法实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

创新互联成立于2013年,先为船山等服务建站,船山等地企业,进行企业商务咨询服务。为船山企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

一、源码

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

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站题目:Java数组复制clone方法实现详解-创新互联
转载来于:http://cdxtjz.cn/article/cdiidh.html

其他资讯