先看看System.arraycopy()的声明:
public static native void arraycopy(Object src,int srcPos, Object dest, int destPos,int length);
src - 源数组。 srcPos - 源数组中的起始位置。 dest - 目标数组。 destPos - 目标数据中的起始位置。 length - 要复制的数组元素的数量。
该方法用了native关键字,说明调用的是其他语言写的底层函数。
再看Arrays.copyOf()
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { @SuppressWarnings("unchecked") 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; }该方法对应不同的数据类型都有各自的重载方法 original - 要复制的数组 newLength - 要返回的副本的长度 newType - 要返回的副本的类型 仔细观察发现,copyOf()内部调用了System.arraycopy()方法
Array.copyOf()可以看作是受限的System.arraycopy(),它主要是用来将原数组全部拷贝到一个新长度的数组,适用于数组扩容。
