Prev Next

Java / List and its implementations

When ArrayList is resized?

While an element is being added to the ArrayList, JVM checks if ArrayList has adequate space available by calling ensureCapacity method. If a space exists, it adds the element to the ArrayList otherwise Array resizing happens.

During resizing process, a new array of a greater size is created, the old array is copied to new array using Arrays.copyOf and the new array is assigned to the existing array.

Below is the formula used to resize the array.

int newCapacity= (oldCapacity * 3)/2 +1;

When we use ArrayList() constructor we get a ArrayList with Size=10. On adding 11th element in the list new Arraylist is created inside ensureCapacity() method and existing array is copied.

The growing factor of ArrayList is 1.5 times when resized.

More Related questions...

Show more question and Answers...


Comments & Discussions