← Back to Core Java Course | Chapter 11: Collections | Lesson 4 of 17

Java ArrayList

An ArrayList is a list that grows and shrinks as you add or remove items, like a stretchy shopping list. You can reach any item by its position.
Syntax
java
ArrayList<Type> list = new ArrayList<>();
list.add(item);
list.get(index);
list.remove(index);
list.size();

Introduction to ArrayList

ArrayList is a resizable array implementation of the List interface — unlike a plain array, it automatically grows as you add elements, so you don't need to know the final size up front.

Example: Introduction to ArrayList

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>(); // grows automatically
		list.add("a");
		list.add("b");
		System.out.println(list);
	}
}

ArrayList Operations

Adding to the end of an ArrayList is fast (amortized O(1)), but inserting or removing from the middle is slower (O(n)) since every following element has to shift over to keep the list contiguous.

Example: ArrayList Operations

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		list.add("c");
		list.add(1, "inserted"); // slower: shifts elements after index 1
		System.out.println(list);
	}
}

Iterating over an ArrayList

You can iterate an ArrayList with a standard for-each loop, an explicit Iterator, or an index-based for loop when you need the current position — each suits different situations depending on whether you need to modify the list mid-iteration.

Example: Iterating over an ArrayList

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		for (String item : list) { // for-each
			System.out.println(item);
		}
		for (int i = 0; i < list.size(); i++) { // index-based
			System.out.println(list.get(i));
		}
	}
}

Common ArrayList Methods

Common ArrayList methods include add(), get(index), remove(), contains(), and size() — together they cover nearly everything you'll need for typical list manipulation without writing custom logic.

Example: Common ArrayList Methods

java
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in list
		ArrayList<String> list = new ArrayList<>();
		list.add("a");
		list.add("b");
		// Print a line to the console
		System.out.println(list.get(0));
		// Print a line to the console
		System.out.println(list.contains("b"));
		System.out.println(list.size());
		list.remove("a");
		System.out.println(list);
	}
}

ArrayList vs Standard Arrays

ArrayList trades the fixed size and slightly better raw performance of a plain array for dynamic resizing and a much richer built-in API — use a plain array only when the size is truly fixed and performance is critical.

Example: ArrayList vs Standard Arrays

java
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		int[] fixedArray = new int[3]; // fixed size
		ArrayList<Integer> dynamicList = new ArrayList<>(); // resizes as needed
		dynamicList.add(1);
		dynamicList.add(2);
		System.out.println(fixedArray.length);
		System.out.println(dynamicList.size());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling get() on an index that does not exist, which throws IndexOutOfBoundsException.
  2. Removing items in a for-each loop, which throws ConcurrentModificationException.
  3. Using remove(1) on a List<Integer> and removing by index when you meant by value.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.