最近被这个方法坑了下,特此放在这里,纪念!!!
/**
* @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. 复制的长度
**/
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
示例(实现移动位置的效果):
int[] sources = new int[]{0, 1, 2, 3, 4, 5, 6, 7};
// 目标是 将 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));
输出结果:

网友评论