Sobes.tech
Junior — Middle

How is the initial capacity of an ArrayList determined when it is created without specifying a size?

sobes.tech AI

Answer from AI

When creating an ArrayList without specifying an initial size, for example new ArrayList<>(), Java uses an internal array with an initial capacity of 10 elements. This means that initially, the ArrayList allocates memory for 10 elements, and no resizing occurs until this number is exceeded.

Example:

ArrayList<String> list = new ArrayList<>();
// Internal array has a size of 10
list.add("item1");
// ...

If the number of elements exceeds the current capacity, the ArrayList automatically increases the size of its internal array, usually by about 1.5 times.

How is the initial capacity of an ArrayList… - sobes.tech