什么更有效:System.arraycopy或Arrays.copyOf?
布洛赫 中的方法同时使用 和 来复制数组。toArray
ArrayList
System.arraycopy
Arrays.copyOf
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
如何比较这两种复制方法,何时应使用哪种复制方法?