定義
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-1。
簡單說,就是數組間的復制
應用
常用作數組的擴容,如ArrayList底層數組的擴容
參數
- Object src:the source array. 源數組
- int srcPos:starting position in the source array. 在源數組中,開始復制的位置
- Object dest:the destination array. 目標數組
- int destPos:starting position in the destination data. 在目標數組中,開始賦值的位置
- int length:the number of array elements to be copied. 被復制的數組元素的數量
過程詳解
以下面這個例子進行分析
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"|
注意,目標數組下標范圍外的元素不會改變!
深復制與淺復制
- 當數組為一維數組,且元素為基本類型或String類型時,屬於深復制,即原數組與新數組的元素不會相互影響
- 當數組為多維數組,或一維數組中的元素為引用類型時,屬於淺復制,原數組與新數組的元素引用指向同一個對象
- 這里說的影響,是兩個數組復制后對應的元素,並不一定是下標對應
- 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類型時,才是深復制,其它都屬於淺復制
下圖是System.arraycopy()復制的過程
異常
ArrayIndexOutOfBoundsException ArrayStoreException NullPointerException
1. ArrayIndexOutOfBoundsException
當數組越界時,拋出異常:ArrayIndexOutOfBoundsException
srcPos < 0 || destPos < 0 || length < 0
src.length < length + srcPos
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) */
最后關於幾種數組復制的效率問題
1、for循環,手動復制
2、System.arraycopy()方法
3、Arrays.copyOf()方法
4、clone()方法
結論:
由於System.arraycopy()是最貼近底層的,其使用的是內存復制,省去了大量的數組尋址訪問等時間,故效率最高。
對於Arrays.copyOf()方法查看源碼可以看到:
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;
}
它是借助System.arraycopy()方法實現的,故效率次於System.arraycopy()
clone()方法效率是最低的,一般需要重寫,clone的方法Object執行特定的克隆操作。 首先,如果此對象的類不實現接口Cloneable ,則拋出CloneNotSupportedException 。 請注意,所有數組都被認為是實現接口Cloneable ,並且數組類型T[]的clone方法的返回類型是T[] ,其中T是任何引用或原始類型。 否則,該方法將創建該對象的類的新實例,並將其所有字段初始化為完全符合該對象的相應字段的內容,就像通過賦值一樣。 這些字段的內容本身不被克隆。 因此,該方法執行該對象的“淺拷貝”,而不是“深度拷貝”操作。如果需要“深度拷貝”操作,則需要遞歸clone()。
對於數組空間小的情況下,前三種差別不大,對於比較大的數組,便可以明顯看出差別。
參考文章:
https://blog.csdn.net/qq_32440951/article/details/78357325
https://blog.csdn.net/balsamspear/article/details/85069207
https://www.cnblogs.com/jay8576/p/9649591.html