System.arraycopy方法:如果是數組比較大,那麼使用System.arraycopy會比較有優勢,因為其使用的是記憶體複製,省去了大量的數組定址訪問等時間
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
複製指定源數組src到目標數組dest。
複製從src的srcPos索引開始,複製的個數是length,複製到dest的索引從destPos開始。
1、基本使用
@Test public void testCopy() { int[] ids = {1, 2, 3, 4, 5}; // 1、測試複製到別的數組上 // 將ids數組的索引從0開始其後5個數,複製到ids2數組的索引從0開始 int[] ids2 = new int[5]; System.arraycopy(ids, 0, ids2, 0, 5); System.out.println(Arrays.toString(ids2)); // [1, 2, 3, 4, 5] // 2、測試自我複製 System.arraycopy(ids, 0, ids, 3, 2); System.out.println(Arrays.toString(ids)); // [1, 2, 3, 1, 2] // 3、如果是類型轉換問題 Object[] o1 = {1, 2, 3, 4.5, 6.7}; Integer[] o2 = new Integer[5]; try { System.arraycopy(o1, 0, o2, 0, o1.length); } catch (ArrayStoreException ex) { // 發生儲存轉換,部分成功的資料會被複製過去 System.out.println("拷貝發生異常:資料轉換錯誤,無法儲存。"); } // 從結果看,前面3個可以複製的資料已經被儲存了。剩下的則沒有 System.out.println(Arrays.toString(o2)); // [1, 2, 3, null, null] }
2、複製後改變複製後的數組
如果是複製一個一維數組,那麼改變複製後的數組並不影響原數組。但是如果複製一個二維數組,那麼改變其中任何一個數組,那麼另一個的值也發生了變化。開始不是很明白,後來上網查了查資料,理解了其中奧妙。
java其實沒有二維數組的概念,平常實現的二維數組只是元素是一維數組的一維數組,而數組也是參考型別,繼承自Object類。數組是new出來的。這些性質也就導致arraycopy()二維數組時出現的問題。
如果是一維數組,那麼元素都是基礎類型(如int,double等),使用arraycopy()方法後,是把原數組的值傳給了新數組,屬於值傳遞。而如果是二維數組,數組的第一維裝的是一個一維數組的引用,第二維裡是元素數值。對二維數組應用arraycopy()方法後,第一維的引用被複製給新數組的第一維,也就是兩個數組的第一維都指向相同的“那些數組”。而這時改變其中任何一個數組的元素的值,其實都修改了“那些數組”的元素的值,所以原數組和新數組的元素值都一樣了。
@Test public void testCopy2(){ int[] s1 = {1, 2, 3, 4, 5}; int[] s2 = new int[5]; System.arraycopy(s1, 0, s2, 0, 5); System.out.println("This is s1"); for(int aS1 : s1) { System.out.print(aS1 + " , "); } s2[2] = 111; System.out.println("\nThis is s2"); for(int aS2 : s2) { System.out.print(aS2 + " , "); } System.out.println("\nThis is s1"); for(int aS1 : s1) { System.out.print(aS1 + " , "); } System.out.println("\n-----------------------"); //二維數組 int[][] s3 = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}; int[][] s4 = new int[s3.length][s3[0].length]; System.out.println("This is s3"); System.arraycopy(s3, 0, s4, 0, s3.length); for (int[] aS3 : s3) { for (int j = 0; j < s4[0].length; j++) { System.out.print(aS3[j] + ","); } } s4[1][3] = 111; System.out.println("\nThis is s4"); for (int[] aS4 : s4) { for (int j = 0; j < s4[0].length; j++) { System.out.print(aS4[j] + ","); } } System.out.println("\nThis is s3"); for (int[] aS3 : s3) { for (int j = 0; j < s4[0].length; j++) { System.out.print(aS3[j] + ","); } } }
結果:This is s1 :1 , 2 , 3 , 4 , 5 ,
This is s2 : 1 , 2 , 111 , 4 , 5 ,
This is s1 : 1 , 2 , 3 , 4 , 5 ,
-----------------------
This is s3 : 1,2,3,4,5,6,7,8,9,10,
This is s4 : 1,2,3,4,5,6,7,8,111,10,
This is s3 : 1,2,3,4,5,6,7,8,111,10, 3、Arrays.copyOf()
該方法對於不同的資料類型都有相應的方法重載。
//複雜資料類型public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }public static <T> T[] copyOf(T[] original, int newLength) { return (T[]) copyOf(original, newLength, original.getClass());}由U類型複製為T類型。
original - 要複製的數組
newLength - 要返回的副本的長度
newType - 要返回的副本的類型
//基礎資料型別 (Elementary Data Type)(其他類似byte,short···) public static int[] copyOf(int[] original, int newLength) { int[] copy = new int[newLength]; System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }觀察其原始碼發現copyOf(),在其內部建立了一個新的數組,然後調用arrayCopy()向其複製內容,返回出去。
總結:
1.copyOf()的實現是用的是arrayCopy();
2.arrayCopy()需要目標數組,對兩個數組的內容進行可能不完全的合併作業。
3. copyOf()在內部建立一個數組,調用arrayCopy()將original內容複寫到copy中去,並且長度為newLength。返回copy;
4.arraycopy 方法會因為新數組大小比久數組大小小而報IndexOutOfBoundsException;
copyOf 則不會因此報錯,因為copyOf 的傳回值是在內部new 好的copy 數組,而該copy 數組new 的大小就等於newLength ,
故即使在用戶端指定好了新數組newArray 的大小,接收到傳回值後也是指向底層new 出來的數組copy 。換句話說( 也可以因此推出其他的區別) ,在用戶端代碼中即使不給新數組new 對象,如:String[] newStr = null;那麼對於arraycopy 是會報NullPointerException 的錯誤的,而對於java.util.Arrays 中的copyOf 方法則由於jdk 底層已經new 出了對象而不會報該錯誤。不過需要特別注意的是:copyOf 方法最後也是調用System.arraycopy 的方法,不過由於前面的準備,異常情況就不會出現了。