What data structure does an ArrayList use internally?
as i understand is that
ArrayList class implements List interface and (as interface only extends other interface) List interface extends Collection interface. while talking about arraylist when we initialize in memory it reserve by default space 10 > and create array of Integer which you normally use. when this this array is full then the another array of interger is created which is greater then default size.
List<Integer> list = new ArrayList<>();
now in memory as => Integer[] list = new Integer[10];
now suppose that you enter 1,2,3,4,5,6,7,8,9,10 array is full now and what happen when you enter 11 in the memory another array of Integer is created which is greater then by default and all element in old array is copied in new array. Internally arraylist user Object[] array.
thats how arrayList work
Basic Data Structure which is used in an ArrayList is –
private transient Object[] elementData;
So, going by declaration it’s an array of Object. When we actually create an arrayList following piece of code is executed –
this.elementData=new Object[initial capacity];
When we create an ArrayList, default constructor is invoked and will internally create an array of Object with default size, which is 10. Now, As we all know that unlike normal arrays, the size of the ArrayList grows dynamically. But how its size grows internally?
So what happens internally is a new Array is created with size 1.5 x currentSize and the data from old Array is copied into this new Array. Every time when it reaches ArrayList at maximum capacity then copy and destroy previous array make new array with new capacity (1.5 x old capacity).
For more details your can read my blog here.