- copyOfRange
public static int[] copyOfRange(int[] original, int from, int to) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); int[] copy = new int[newLength]; System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy; }
- 内部实现实际是调用了System.arraycopy数组拷贝方法 - Math.min(original.length - from, newLength)这行代码表示,若拷 贝的内容超出源数组的数组边界,则只拷贝from位置到源数组最后一个元素,防 止 数组越界
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)