ArrayList中的toArray方法,Bloch使用System.arraycopy和Arrays.copyOf来复制数组。什么更有效率:System.arraycopy vs 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;
}
如何比较这两种复制方式,何时使用哪种?
什么是 “布洛赫”?为什么代码片段相关? – 2015-05-02 13:56:23
@Ciro Bloch是编写ArrayList实现的人。 – Insomniac 2015-06-04 23:33:14
另请参见:https://stackoverflow.com/q/44487304/14955 – Thilo 2017-06-12 03:14:39