Let’s learn ArrayList in java.
ArrayList in java
Underlying data structure for ArrayList is resizable array or growable array. It handles resizing automatically and internally maintains size of elements but also the capacity, the amount of memory that is reserved.
- In ArrayList duplicates are allowed.
- ArrayList preserves insertion order.
- ArrayList can hold heterogeneous objects.
- null insertion is possible.
- Is part of collection framework or interface.
- ArrayList implements List.
- Every methods present in ArrayList are non-synchronized.
- For frequent retrieving operation, ArrayList is the best. Because ArrayList implements RandomAccess interface.
- ArrayList cannot hold primitive data types such as int, double, char, and long.
- At a time multiple threads are allowed to operate on ArrayList object and hence it is not thread safe.
- ArrayList class implements RandomAccess interface.
Here’s arraylist in java example.
import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add("Ajay"); al.add(15); al.add("Ajay"); al.add(null); System.out.println(al); al.remove(2); System.out.println(al); al.add(2, "Mahesh"); al.add("Nithin"); System.out.println(al); } }
Output:
[Ajay, 15, Ajay, null]
[Ajay, 15, null]
[Ajay, 15, Mahesh, null, Nithin]
ArrayList methods
Methods | Description |
boolean addAll(Collection <? extends E> c) | appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator. |
void add(int index, E element) | inserts the specified element at the specified position in this list. |
boolean addAll( int index, Collection <? extends E> c) | inserts all of the elements in the specified collection into this list at the specified position. |
void trimToSize() | trim the capacity of the instance of the ArrayList to the list’s current size. |
boolean remove(Object o) | removes the first occurrence of the specified element from this list, if it is present. |
boolean removeAll(Collection<?> c) | removes from this list all of its elements that are contained in the specified collection. |
boolean retainAll(Collection<?> c) | retains only the elements in this list that are contained the specified collection. |
Object clone() | returns a shallow copy of this ArrayList instance. |
boolean contains(Object o) | returns true if this list contains the specified element. |
boolean containsAll(Collection c) | returns true if this list contains all of the elements of the specified collection. |
E get(int index) | returns the element at the specified position in this list. |
int indexOf(Object o) | returns the index of the first occurrence of the specified element in this list, or – 1 if this list does not contain the element. |
int lastIndexOf(Object o) | returns the index of the last occurrence of the specified element in this list, or – 1 if this list does not contain the element. |
void clear() | removes all of the elements from this list. |
boolean isEmpty() | returns true if this list contains no elements. |
int size() | returns the number of elements in this list. |
List<E> subList(int fromIndex, int toIndex) | returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. |
Iterator<E> iterator() | returns an iterator over the elements in this list in proper sequence. |
ListIterator<E> listIterator() | returns a list iterator over the elements in this list (in proper sequence). |
ListIterator<E> listIterator(int index) | returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list. |
Object[] toArray() | returns an array containing all of the elements in this list in proper sequence (from first to last element). |
void ensureCapacity(int minCapacity) | Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. |
void forEach(Consumer<? super E> action) | Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. |
protected void removeRange(int fromIndex, int toIndex) | Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. |
E set(int index, E element) | Replaces the element at the specified position in this list with the specified element. |
Spliterator<E> spliterator() | Creates a late-binding and fail-fast Spliterator over the elements in this list. |
ArrayList constructors
ArrayList l = new ArrayList();
creates an empty ArrayList object with default initial capacity 10. Once arraylist reaches its maximum capacity then a new arraylist object is created with new capacity = (current capacity * 3 /2) + 1
ArrayList l = new ArrayList(int initialcapacity);
creates an empty arraylist object with specified initial capacity.
ArrayList l = new ArrayList(Collection c);
creates an equivalent arraylist object for the given Collection.
Hierarchy of arraylist

Loop through arraylist java
Let’s see how to iterate or loop through ArrayList using for-each loop and for loop.
import java.util.ArrayList; import java.util.Iterator; import java.util.ListIterator; public class ArrayListIterateDemo { public static void main(String[] args) { ArrayList<String> birds = new ArrayList<String>(); birds.add("Sparrow"); birds.add("Pigeon"); birds.add("Peacock"); birds.add("Eagle"); System.out.println("Using for-each loop: "); for(String s: birds) { System.out.println(s); } System.out.println("Using for loop: "); for(int a = 0; a < birds.size(); a++) { System.out.println(birds.get(a)); } } }
Output:
Using for-each loop:
Sparrow
Pigeon
Peacock
Eagle
Using for loop:
Sparrow
Pigeon
Peacock
Eagle
Reference – Oracle docs