Time cost of addAll () method of list in Java

public boolean addAll(Collection<? extends E> c) {
    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacityInternal(size + numNew);  // Increments modCount
    System.arraycopy(a, 0, elementData, size, numNew);
    size += numNew;
    return numNew != 0;
}

I see that the time cost of addAll () is proportional to the number of inserts. I think it should be the same no matter how many inserts it is, or what is the mystery of System.arraycopy, the native method? please explain

Mar.07,2021

A new array is generated when inserted, and there is a copy operation. The larger the array, the longer it takes.

Menu