June 2018
Beginner
722 pages
18h 47m
English
The ArrayList class is named so because its implementation is based on an array. It actually uses an array behind the scenes. If you right-click on ArrayList in IDE and view the source code, here is what you are going to see:
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;}
It is just a wrapper around the array Object[]. And here is how method add(E) is implemented, for example:
public boolean add(E e) { modCount++; add(e, elementData, size); return true;}private void add(E e, Object[] elementData, int s) { if (s == elementData.length) elementData = grow(); elementData[s] = e; size = s + 1;}
And if you study the source ...
Read now
Unlock full access