Java ArrayList Class

Last updated on June 18th, 2025 at 12:36 pm

  • ArrayList class is sub class of collection interface which implements to List interface.
  • ArrayList class provides resizable-array so it can grow automatically as per requirement which resolves fixed Array limitation where you have to pre-define the size of array.
  • It can hold null elements too.
  • It is not synchronized implementation so if multiple threads accessing it concurrently and any one from them modifies the entry then it must be synchronized externally.
  • Also it can hold duplicate elements.
Java ArrayList Class hierarchy

Important methods of ArrayList class are as below.

  • boolean add(E e) : It will append given element at the end of this ArrayList.
  • void add(int index, E element) : It will add specified element at the given index in list.
  • boolean addAll(Collection<? extends E> c) : It will append all the elements of given collection at the end of array list. Appending order will be as per returned by collection’s iterator.
  • boolean addAll(int index, Collection<? extends E> c) : It will insert all elements of specified collection in ArrayList starting from given index.
  • void clear() : It will clear List by removing all elements from it.
  • Object clone() : It will return shallow copy of ArrayList.
  • boolean contains(Object o) : It will return true if given element is present in list.
  • void ensureCapacity(int minCapacity) : It will increases the capacity of array list if required to make sure that it can store the elements as per given minCapacity argument.
  • E get(int index) : It will get element from specified inced from array list.
  • int indexOf(Object o) : It will return the index of specified object(first occurrence) from array list.
  • boolean isEmpty() : It will check and return true if array list is empty.
  • Iterator<E> iterator() : It will return an iterator over the elements from array list.
  • int lastIndexOf(Object o) : It will return index of last occurrence of specified element.
  • ListIterator<E> listIterator() : It will return the list iterator over the elements of array list.
  • ListIterator<E> listIterator(int index) : It will return the list iterator over the elements of array list starting from specified index.
  • E remove(int index) : It will remove element from array list which is stored at specified index.
  • boolean remove(Object o) : It will remove specified element(first occurrence) from array list.
  • boolean removeAll(Collection<?> c) : It will remove all elements from list which are specified in given collection.
  • protected void removeRange(int fromIndex, int toIndex) : It will remove elements from list starting from fromIndex to toIndex.
  • boolean retainAll(Collection<?> c) : It will retain only those elements in array list which are specified in given collection.
  • E set(int index, E element) : It will replace given element in array list at specified position.
  • int size() : It will return size of array list.
  • List<E> subList(int fromIndex, int toIndex) : It will return sub list of elements from this list starting from fromIndex(inclusive) to toIndex(exclusive).
  • Object[] toArray() :It will return proper sequential array of all array list elements.
  • void trimToSize() :It will trim size of array list as per it’s current size.
Below given example will show you usage of ArrayList class’s different methods.

ArrayListExample

package JAVAExamples;

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListClassExample {

 public static void main(String[] args) {
  // Create array list.
  ArrayList<String> a = new ArrayList<String>();
  // Add elements in array list.
  a.add("one");
  a.add("two");
  a.add("three");
  a.add("four");
  a.add("one");
  a.add("six");
  a.add("seven");
  a.add("eight");

  //Iterating over the arraylist elements.
  Iterator itr = a.iterator();
  System.out.println("ArrayList items are : ");
  while (itr.hasNext()) {
   System.out.println(itr.next());
  }
  
  //Add item at specified index.
  a.add(5, "nine");
  System.out.println("Now ArrayList items are : "+a);
  
  //Get item from specified index.
  System.out.println("Item at index 6 is : "+a.get(6));
  
  //Remove item from array list.
  a.remove("three");
  System.out.println("Now ArrayList items are : "+a);
  
  //Get index of element from array list.
  System.out.println("Index of one is : "+a.indexOf("one"));
  
  //Get last index of element from array list.
  System.out.println("Last Index of one is : "+a.lastIndexOf("one"));
 }
}

Output :

ArrayList items are : 
one
two
three
four
one
six
seven
eight
Now ArrayList items are : [one, two, three, four, one, nine, six, seven, eight]
Item at index 6 is : six
Now ArrayList items are : [one, two, four, one, nine, six, seven, eight]
Index of one is : 0
Last Index of one is : 3

author avatar
Aravind QA Automation Engineer & Technical Blogger
Aravind is a QA Automation Engineer and technical blogger specializing in Playwright, Selenium, and AI in software testing. He shares practical tutorials to help QA professionals improve their automation skills.
Stay Updated with New Articles
Get the latest tutorials and insights delivered to your inbox.

Leave a Reply

Your email address will not be published. Required fields are marked *