Java 陣列複製clone方法實現詳解
阿新 • • 發佈:2020-01-07
這篇文章主要介紹了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.等號復值後修改一個後會影響另一個
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。