ArrayList

- ArrayList is internally created as an Array
- As seen from the above screenshot, it is saved in this.elementData array


- Initially, the size of the ArrayList is 0 and the elementData length is 0 as well.
- However when the first element is added, the size of the ArrayList is 1 while the elementData length is 10(DEFAULT_CAPACITY).

- If we add elements over the capacity, a new array will be created with larger capacity and the contents will be copied over.
- The original array will be left over for garbage collection.
- As seen from the above code, every time the array list resizes, the capacity will grow by half of its current size. (newSize = currentSize * 1.5)
- As this resizing process consumes resources, if you know approximately how many items will be stored in the ArrayList, it is better to specify the capacity when initializing the ArrayList.
- e.g)
new ArrayList<>(CAPACITY)
- e.g)

Leave a comment