照现在的赋值int arr2[][]=Arrays.copyOfRange(arr, 3,4); 应该每次都是空指针而不是有时
看下源码
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
看下源码
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
int newLength = to - from;
if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, from, copy, 0,
Math.min(original.length - from, newLength));
return copy;
}
//复制第一个
Arrays.copyOfRange(arr, 0,1);
//赋值前两个
Arrays.copyOfRange(arr, 0,2);
//赋值前三个
Arrays.copyOfRange(arr, 0,3);
……
copyOfRange这个方法中的第二个元素可以看作是arr的下标(但是这个值的取值范围是[0,arr.length])
而要复制的长度为第三个参数减去第二个参数