java.lang.System.arraycopy在jdk中的定義
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos,int length);
作用:從指定的源數組(從*指定位置開始)複製一個數組到目標數組的指定位置。(native關鍵字修飾)
參數:
Object src
- 源數組。
int srcPos
- 源數組中的起始位置。
Object dest
- 目標數組。
int destPos
- 目標數組中的起始位置。
int length
- 要複製的源數組元素的數量。
舉例:
原數組:int[] arr={1,2,3,4,5,6};
目標數組:int[] Arr=new int[10];
操作:將arr原數組第2個位置以後的4個數據copy到arr2目標數組第3個位置:
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 4, 5, 6};
Integer[] arr2 = new Integer[10];
System.out.println("將arr原數組第2個位置以後的4個數據copy到arr2目標數組第3個位置:");
System.out.println("arr原數組:");
for (Integer integer : arr) {
System.out.println(integer);
}
java.lang.System.arraycopy(arr, 1, arr2, 2, 4);
System.out.println("數據複製之後,arr2目標數組:");
for (Integer integer1 : arr2) {
System.out.println(integer1);
}
}
如下圖: