1. Introduction

In Java, ArrayList is a resizable array implementation of the List interface. Two concepts often associated with an ArrayList are its capacity and its size (length). The capacity refers to the length of the internal array used to store elements, while the size indicates the actual number of elements in the ArrayList. This blog post demonstrates the difference between capacity and size in an ArrayList in Java.

2. Program Steps

1. Create an ArrayList with a specific initial capacity.

2. Add some elements to the ArrayList.

3. Print out the size of the ArrayList.

4. Reflectively obtain and print out the capacity of the ArrayList.

5. Add more elements and observe the changes in size and capacity.

3. Code Program

import java.lang.reflect.Field;
import java.util.ArrayList;

public class CapacityVsSize {

    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {

        // Step 1: Create an ArrayList with a specific initial capacity
        ArrayList<Integer> arrayList = new ArrayList<>(5);

        // Step 2: Add some elements to the ArrayList
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);

        // Step 3: Print out the size of the ArrayList
        System.out.println("Size of ArrayList: " + arrayList.size());

        // Step 4: Reflectively obtain and print out the capacity of the ArrayList
        Field arrayField = ArrayList.class.getDeclaredField("elementData");
        arrayField.setAccessible(true);
        Object[] array = (Object[]) arrayField.get(arrayList);
        System.out.println("Capacity of ArrayList: " + array.length);

        // Step 5: Add more elements and observe the changes in size and capacity
        arrayList.add(4);
        arrayList.add(5);
        arrayList.add(6);

        System.out.println("Size of ArrayList after adding more elements: " + arrayList.size());
        array = (Object[]) arrayField.get(arrayList);
        System.out.println("Capacity of ArrayList after adding more elements: " + array.length);
    }
}

Output:

Size of ArrayList: 3
Capacity of ArrayList: 5
Size of ArrayList after adding more elements: 6
Capacity of ArrayList after adding more elements: 10

4. Step By Step Explanation

Step 1: An ArrayList named arrayList is created with a specified initial capacity of 5.

Step 2: Three elements are added to the ArrayList, so the size of the ArrayList is now 3.

Step 3: The size() method of the ArrayList is used to print the current number of elements in the ArrayList: "Size of ArrayList: 3".

Step 4: Reflection is used to access the private elementData field of the ArrayList class, which holds the internal array. The length of this internal array represents the capacity of the ArrayList, and it is printed out: "Capacity of ArrayList: 5".

Step 5: Three more elements are added to the ArrayList, making the size increase to 6. However, since the initial capacity was 5, the ArrayList needs to grow its capacity to accommodate the new elements. The capacity is increased to 10, which is printed out along with the updated size.