Quantcast
Viewing latest article 10
Browse Latest Browse All 11

What is more efficient: System.arraycopy or Arrays.copyOf?

The toArray method in ArrayList, Bloch uses both System.arraycopy and Arrays.copyOf to copy an array.

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;
}

How can I compare these two copy methods and when should I use which?


Viewing latest article 10
Browse Latest Browse All 11

Trending Articles