System.arraycopy()方法詳解-jdk1.8

定義java

public static native void arraycopy(Object src,  int srcPos, Object dest, int destPos, int length);

能夠看到,它是一個靜態本地方法,由虛擬機實現,效率天然比用java一個個複製高數組

方法含義
從源數組src取元素,範圍爲下標srcPos到srcPos+length-1,取出共length個元素,存放到目標數組中,存放位置爲下標destPos到destPos+length-1spa

簡單說,就是數組間的複製code

應用
經常使用做數組的擴容,如ArrayList底層數組的擴容對象

參數繼承

  1. Object src:the source array. 源數組
  2. int srcPos:starting position in the source array. 在源數組中,開始複製的位置
  3. Object dest:the destination array. 目標數組
  4. int destPos:starting position in the destination data. 在目標數組中,開始賦值的位置
  5. int length:the number of array elements to be copied. 被複制的數組元素的數量

過程詳解
如下面這個例子進行分析element

public class SystemArrayCopy {

    public static void main(String[] args) {
        int[] src = {1, 2, 3, 4};
        int[] dest = new int[5];
        System.arraycopy(src, 0, dest, 1, 4);

        for (Object o : dest) {
            System.out.println(o);
        }
    }
}
/*
0 1 2 3 4
*/
src = |1|2|3|4|
dest = |0|0|0|0|0|
執行System.arraycopy(src, 0, dest, 1, 4);時
第一步:從源數組(src)中,從下標0開始取,取4個,也就是src[0]-src[3],即1 2 3 4四個數
第二步:把取出的數,按順序,存放到目標數組(dest)中,從下標1開始存,存4個,也就是dest[1]-dest[4]
因此數組dest爲:|0|1|2|3|4|

再來一個例子字符串

public class SystemArrayCopy {

    public static void main(String[] args) {
        String[] src = {"aa", "bb", "cc", "cc"};
        String[] dest = new String[]{"a", "b", "c", "d", "e"};
        System.arraycopy(src, 1, dest, 2, 2);

        for (Object o : dest) {
            System.out.println(o);
        }
    }
}
/*
a b bb cc e
*/
String[] src = |"aa"|"bb"|"cc"|"cc"|
String[] dest = |"a"|"b"|"c"|"d"|"e"|
執行 System.arraycopy(src, 1, dest, 2, 2);時
第一步:從源數組(src)中,從下標1開始取,取2個,也就是src[1]-src[2],即"bb" "cc"兩個字符串
第二步:把取出的數,按順序,存放到目標數組(dest)中,從下標2開始存,存2個,也就是dest[2]-dest[3]
因此數組dest爲:|"a"|"b"|"bb"|"cc"|"e"|

注意,目標數組下標範圍外的元素不會改變!get

深複製與淺複製

  1. 當數組爲一維數組,且元素爲基本類型或String類型時,屬於深複製,即原數組與新數組的元素不會相互影響
  2. 當數組爲多維數組,或一維數組中的元素爲引用類型時,屬於淺複製,原數組與新數組的元素引用指向同一個對象
  1. 這裏說的影響,是兩個數組複製後對應的元素,並不必定是下標對應
  2. String的特殊是由於它的不可變性

1. 一維數組,元素爲基本類型虛擬機

public class SystemArrayCopy {

    public static void main(String[] args) {
        String str1 = "aa";
        String str2 = "bb";
        String str3 = "cc";
        String str4 = "dd";

        String[] src = {str1, str2, str3, str4};
        String[] dest = new String[4];

        System.arraycopy(src, 0, dest, 0, 4);

        System.out.println("改變前");
        print("src = ", src);
        print("dest = ", dest);

        src[0] = "abcd";

        System.out.println("改變後");
        print("src = ", src);
        print("dest = ", dest);
    }

    private static void print(String string, String[] arr) {
        System.out.print(string);
        for (String str : arr) {
            System.out.print(str + " ");
        }
        System.out.println();
    }

}
/*
改變前
src = aa bb cc dd 
dest = aa bb cc dd 
改變後
src = abcd bb cc dd 
dest = aa bb cc dd 
*/

能夠看到,源數組第0個元素改變,並不會影響到目標數組

2. 多維數組

public class SystemArrayCopy {

    public static void main(String[] args) {
        int[] arr1 = {1, 2};
        int[] arr2 = {3, 4};
        int[] arr3 = {5, 6};
        int[] arr4 = {7, 8};

        int[][] src = new int[][]{arr1, arr2, arr3, arr4};
        int[][] dest = new int[4][];

        System.arraycopy(src, 0, dest, 0, 4);

        System.out.println("改變前");
        print("src = ", src);
        print("dest = ", dest);

        src[0][0] = 11111;

        System.out.println("改變後");
        print("src = ", src);
        print("dest = ", dest);
    }

    // 簡單輸出二維int數組的方法
    private static void print(String string, int[][] arr) {
        System.out.print(string);
        for (int[] a : arr) {
            for (int i : a) {
                System.out.print(i + " ");
            }
            System.out.print(",");
        }
        System.out.println();
    }
}
/*
改變前
src = 1 2 ,3 4 ,5 6 ,7 8 ,
dest = 1 2 ,3 4 ,5 6 ,7 8 ,
改變後
src = 11111 2 ,3 4 ,5 6 ,7 8 ,
dest = 11111 2 ,3 4 ,5 6 ,7 8 ,
*/

源數組改變後,目標數組也跟改變了,這就是淺複製

3. 一維數組,元素爲引用類型

public class SystemArrayCopy {

    public static void main(String[] args) {
        People p1 = new People(11, "A");
        People p2 = new People(12, "B");
        People p3 = new People(13, "C");
        People p4 = new People(14, "D");

        People[] src = new People[]{p1, p2, p3, p4};
        People[] dest = new People[4];

        System.arraycopy(src, 0, dest, 0, 4);

        System.out.println("改變前");
        print("src = ", src);
        print("dest = ", dest);

        src[0].setAge(111);
        src[0].setName("AAA");

        System.out.println("改變後");
        print("src = ", src);
        print("dest = ", dest);
    }

    private static void print(String string, People[] arr) {
        System.out.print(string);
        for (People p : arr) {
            System.out.print(p + ", ");
        }
        System.out.println();
    }
}
public class People {
    private int age;
    private String name;

    // get set constructor toString
}
/*
改變前
src = People{age=11, name='A'}, People{age=12, name='B'}, People{age=13, name='C'}, People{age=14, name='D'}, 
dest = People{age=11, name='A'}, People{age=12, name='B'}, People{age=13, name='C'}, People{age=14, name='D'}, 
改變後
src = People{age=111, name='AAA'}, People{age=12, name='B'}, People{age=13, name='C'}, People{age=14, name='D'}, 
dest = People{age=111, name='AAA'}, People{age=12, name='B'}, People{age=13, name='C'}, People{age=14, name='D'}, 
*/

源數組改變後,目標數組也跟改變了,這就是淺複製

總結:只有數組爲一維數組,而且元素爲基本類型或String類型時,纔是深複製,其它都屬於淺複製

異常
ArrayIndexOutOfBoundsException ArrayStoreException NullPointerException

1. ArrayIndexOutOfBoundsException
當數組越界時,拋出異常:ArrayIndexOutOfBoundsException

  1. srcPos < 0 || destPos < 0 || length < 0
  2. src.length < length + srcPos
  3. dest.length < length + destPos

代碼演示

// 1. srcPos < 0 || destPos < 0 || length < 0
public class SystemArrayCopy {

    public static void main(String[] args) {
        int[] src = {1, 2, 3, 4};
        int[] dest = new int[5];
        System.arraycopy(src, -1, dest, 0, 0);
        
        for (Object o : dest) {
            System.out.println(o);
        }
    }
}
/*
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at com.balsam.sources.SystemArrayCopy.main(SystemArrayCopy.java:13)
*/

// 2. src.length < length + srcPos
public class SystemArrayCopy {

    public static void main(String[] args) {
        int[] src = {1, 2, 3, 4};
        int[] dest = new int[5];
        System.arraycopy(arr, 1, dest, 0, 4);
        
        for (Object o : dest) {
            System.out.println(o);
        }
    }
}
/*
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at com.balsam.sources.SystemArrayCopy.main(SystemArrayCopy.java:13)
*/

// 3. dest.length < length + destPos
public class SystemArrayCopy {

    public static void main(String[] args) {
        int[] src = {1, 2, 3, 4};
        int[] dest = new int[3];
        System.arraycopy(src, 0, dest, 0, 4);
        
        for (Object o : dest) {
            System.out.println(o);
        }
    }
}
/*
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at com.balsam.sources.SystemArrayCopy.main(SystemArrayCopy.java:13)
*/

2. ArrayStoreException
當兩數據的元素類型不匹配時,拋出異常:ArrayStoreException

src元素爲dest元素的子類時,是能夠複製的,特殊地,int不是Object的子類(或者說,不存在繼承的概念),因此,下例中,把int[] src = {1, 2, 3, 4};改成Integer[] src = {1, 2, 3, 4};,是不會報錯的

public class SystemArrayCopy {

    public static void main(String[] args) {
        int[] src = {1, 2, 3, 4};
        Object[] dest = new Object[3];
        System.arraycopy(src, 0, dest, 0, 4);
        
        for (Object o : dest) {
            System.out.println(o);
        }
    }
}
/*
Exception in thread "main" java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at com.balsam.sources.SystemArrayCopy.main(SystemArrayCopy.java:13)
*/

public class SystemArrayCopy {

    public static void main(String[] args) {
        Object[] src = {1, 2, 3, 4};
        int[] dest = new int[3];
        System.arraycopy(src, 0, dest, 0, 4);

        for (Object o : dest) {
            System.out.println(o);
        }
    }
}
/*
Exception in thread "main" java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at com.balsam.sources.SystemArrayCopy.main(SystemArrayCopy.java:13)
*/

3. NullPointerException
當兩個數組,有一個爲null時,拋出異常:NullPointerException

public class SystemArrayCopy {

    public static void main(String[] args) {
        int[] src = {1, 2, 3, 4};
        int[] dest = null;
        System.arraycopy(src, 0, dest, 0, 4);

        for (Object o : dest) {
            System.out.println(o);
        }
    }
}
/*
Exception in thread "main" java.lang.NullPointerException
    at java.lang.System.arraycopy(Native Method)
    at com.balsam.sources.SystemArrayCopy.main(SystemArrayCopy.java:13)
*/
相關文章
相關標籤/搜索