1. 程式人生 > >Java方法之--System.arraycopy方法和Arrays.copyOf()

Java方法之--System.arraycopy方法和Arrays.copyOf()

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 - 要返回的副本的型別
  //基本資料型別(其他類似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 的方法,不過由於前面的準備,異常情況就不會出現了。