Sobes.tech
Junior

What is the initial size of an ArrayList in Java?

sobes.tech AI

Answer from AI

In Java, the ArrayList class by default is created with an initial capacity of 10. This means that the internal array, which stores the elements, can initially hold 10 objects without needing to expand.

Example of creating an ArrayList with default capacity:

ArrayList<String> list = new ArrayList<>(); // initial capacity = 10

If it is known that the list will contain a large number of elements, you can explicitly set the initial capacity to avoid frequent memory reallocations:

ArrayList<String> list = new ArrayList<>(50); // initial capacity 50