ArrayList: Inserting and Retrieving Elements
Let's see how we insert and retrieve elements from an ArrayList.
Inserting into an ArrayList
There are four ways to add elements in an ArrayList:
Inserting a single element at the end
To add a single element at the end of the List, the add(E e)
method can be used, where E
refers to any type of object. This method will check if there is sufficient capacity in the ArrayList. If the ArrayList is full, then it will resize it and insert the element at the end.