ArrayList is a resizable array or dynamic array implementation in java. ArrayList grows dynamically and ensures that there is always a space to add elements. The backing data structure of ArrayList is an array of Object class. ArrayList class in Java has 3 constructors. It has its own version of readObject and writeObject methods. Object Array in ArrayList is transient . It implements RandomAccess, Cloneable, java.io.Serializable.
Internally an ArrayList uses an Object Array which is an array of objects. All operation like deleting, adding and updating the elements happens in this Object array.
ArrayList is one of the most used data structure in Java today.
ArrayList<String> fruits = new ArrayList<>(2); fruits.add("apple"); fruits.add("banana"); // by adding this, the internal array is doubled it's size fruits.add("cantalope"); System.out.println(fruits.toString());
Array is a fixed size data structure while ArrayList is not. Even if we specify some initial capacity, we can add more elements.
If we try to add an element using the add() method in the array list, Internally it checks for the capacity to store the new element, If the internal array is full then a new capacity(more than old capacity by 50% at least) is calculated and a new array is created and copied over the old array to it.
The time complexity of the common operations in ArrayList java.