**/*
* @param src the source array.源陣列
* @param srcPos starting position in the source array.源陣列要複製的起始位置
* @param dest the destination array.目標陣列(將原陣列複製到目標陣列)
* @param destPos starting position in the destination data.目標陣列起始位置(從目標陣列的哪個下標開始複製操作)
* @param length the number of array elements to be copied.複製源陣列的長度
* @exception IndexOutOfBoundsException if copying would cause
* access of data outside array bounds.
* @exception ArrayStoreException if an element in the <code>src</code>
* array could not be stored into the <code>dest</code> array
* because of a type mismatch.
* @exception NullPointerException if either <code>src</code> or
* <code>dest</code> is <code>null</code>.
*/
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,int length);**
例子如下:
package test.demo;
public class ArrayCopyTest {
public static void main(String[] args) {
char[] src = new String("hellow").toCharArray();
char[] dest = new String("12345789").toCharArray();
System.out.print("src源陣列為:");
for(char c : src){
System.out.print(c);
}
System.out.print("
dest目標陣列為:");
for(char c : dest){
System.out.print(c);
}
/*
* 開始執行陣列複製操作
* 將源陣列[`h`,`e`,`l`,`l`,`o`,`w`]從陣列下標0開始的4位長度的陣列[`h`,`e`,`l`,`l`]
* 複製到目標陣列[`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`],從下標為3的位置開始
*/
System.arraycopy(src,0,dest,3,4);
System.out.print("
複製完成之後的目標陣列為:");
for(char c : dest){
System.out.print(c);
}
}
}
結果輸出如下:
src源陣列為:hellow
dest目標陣列為:12345789
複製完成之後的dest目標陣列為:123hell9