Looking to the source:
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
The ArrayList(Collection)
constructor calls toArray()
which calls 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;
}
The important part here is System.arraycopy
which is a native call. Another question on SO addresses the time complexity of this call, and the conclusion seems to be that this call is O(n) in worst case scenarios but that some systems may use block copies for some data types (like primitives I imagine), thus resulting in more efficient runtime than for loop iteration.