ArrayList Class in Java Tutorial with Example

ArrayList class implements List interface. Due to dynamic nature of ArrayList most developers choose ArrayList over Array. It is a resizable array implementation of the List interface. It implements all optional list operations,and permits all elements, including null.

The issue with Array is that they are of fixed length so if it is full you can not resize it and add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same  as it doesn't shrink. On the other hand ArrayList can dynamically grow and shrink after addition and removal operations.

This class is roughly equivalent to Vector, except that it is unsynchronized.The iterators returned by this class's iterator are fail-fast.

There is a list of several ArrayList tutorials at the end of this guide, refer it to gain the understanding of ArrayList class.



ArrayList Example in Java



import java.util.*;

 public class ArrayListExample {
    public static void main(String args[]) {
        
          /*Declaration of ArrayList */
   ArrayList<String> al = new ArrayList<String>();

   /*Elements  added to the ArrayList*/
   al.add("Apple");
   al.add("Pear");
   al.add("Banana");
   al.add("PineApple");
   al.add("Orange");

   /* Showing ArrayList elements */
   System.out.println("ArrayList contains: "+al);

   /*Add element at the given index*/
   al.add(0, "Blackberry");
   al.add(1, "Kiwi");

   /*Remove elements from array list like this*/
   al.remove("Pear");
   al.remove("Orange");

   System.out.println("Current ArrayList is: "+al);

   /*Remove element from the given index*/
   al.remove(1);

   System.out.println("Updated ArrayList is:"+al);
  }
 }


Output
ArrayList contains: [Apple, Pear, Banana, PineApple, Orange]
Current ArrayList is: [Blackberry, Kiwi, Apple, Banana, PineApple]
Updated ArrayList is:[Blackberry, Apple, Banana, PineApple]


ArrayList Tutorials

Please find the list of ArrayList tutorials below :

ArrayList Basics

  1. Initialize ArrayList
  2. Iterate (Loop) ArrayList
  3. Find Length of ArrayList

ArrayList Sorting

  1. Sort ArrayList in Ascending Order
  2. Sort ArrayList in Descending Order
  3. Sort ArrayList of Objects using Comparable and Comparator

ArrayList Add/Remove

  1. Add element to ArrayList
  2. Add element at Particular Index of ArrayList
  3. Append Collection elements to ArrayList
  4. Insert all the Collection elements at the Specified Position in ArrayList
  5. Remove element from Specified Index in ArrayList
  6. Remove specified element from ArrayList

ArrayList Internal Working

  1. How Add() method works Internally in ArrayList

ArrayList Conversions

  1. Convert HashSet to ArrayList
  2. Convert Array to ArrayList
  3. Convert LinkedList to ArrayList
  4. Convert ArrayList to String Array

Differences

  1. ArrayList vs Array
  2. ArrayList vs Vector
  3. ArrayList vs LinkedList
  4. ArrayList vs HashMap
  5. ArrayList vs HashSet
  6. ArrayList vs CopyOnWriteArrayList

Other Tutorials

  1. Synchronize ArrayList
  2. Serialize ArrayList
  3. How to Reverse ArrayList 

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry