最近被這個(gè)方法坑了下,特此放在這里,紀(jì)念!!!
/**
* @param src the source array. 源數(shù)組
* @param srcPos starting position in the source array. 源數(shù)組的起始位置
* @param dest the destination array. 目標(biāo)數(shù)組
* @param destPos starting position in the destination data. 目標(biāo)數(shù)組的起始位置
* @param length the number of array elements to be copied. 復(fù)制的長度
**/
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
示例(實(shí)現(xiàn)移動(dòng)位置的效果):
int[] sources = new int[]{0, 1, 2, 3, 4, 5, 6, 7};
// 目標(biāo)是 將 5 移到 0 去,也就是輸出 5,0,1,2,3,4,6,7
int srcPos = 0;
int destPos = 5;
int destValue = sources[destPos];
System.out.println(Arrays.toString(sources));
System.arraycopy(sources, srcPos, sources, srcPos + 1, destPos - srcPos);
sources[srcPos] = destValue;
System.out.println(Arrays.toString(sources));
輸出結(jié)果:

輸出