标签:
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
arraycopy是个本地方法,无返回值。
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
T[] copy = ((Object)newType == (Object)Object[].class)
? (T[]) new Object[newLength]
: (T[]) Array.newInstance(newType.getComponentType(), newLength);
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
public static <T> T[] copyOf(T[] original, int newLength) {
return (T[]) copyOf(original, newLength, original.getClass());
}
copyOf()底层调用arraycopy,不过可以直接返回一个数组,代码更加简短,只是自定义数组长度的能力更差了。
Java数组的复制Arrays.copyOf()和System.arraycopy()函数
标签:
原文地址:http://www.cnblogs.com/tonyluis/p/5774889.html