// 方法一:使用API中System类的数组复制方法
public static int[] copyOfRange(int[] arr, int from, int to) {
int[] newArr = new int[to - from];
System.arraycopy(arr, from, newArr, 0, to - from);
return newArr;
}
// 方法二:for循环遍历源数组复制指定索引的元素给新数组
public static int[] copyOfRange1(int[] arr, int from, int to) {
int[] newArr = new int[to - from];
for (int i = from, j = 0; i < to; i++, j++) {
newArr[j] = arr[i];
}
return newArr;
}
|