Java ArrayList Explained: Usage, Key Methods, and Practical Examples
What is ArrayList in Java?
ArrayList is a dynamic array that automatically expands and contracts as elements are added or removed. It’s essential when you need a flexible collection that can grow or shrink during runtime.
Unlike a fixed‑size array (think of a rigid rope), an ArrayList behaves like a stretchable rubber band, allowing you to store more items without having to predefine a maximum size.

When you add elements, ArrayList grows automatically; when elements are removed, it shrinks, ensuring efficient memory usage.
ArrayList Methods
- add(Object o) – Appends an element to the end of the list or inserts it at a specified index.
- remove(Object o) – Removes the first occurrence of the specified element.
- remove(int index) – Deletes the element at the given index.
- size() – Returns the current number of elements in the list.
- contains(Object o) – Checks whether the list contains the specified element.
Java ArrayList Example
Below is a concise demonstration of the most common operations on an ArrayList.
import java.util.ArrayList;
public class TestArrayList {
public static void main(String[] args) {
// Create a generic ArrayList
ArrayList<String> arlTest = new ArrayList<>();
// Initial size
System.out.println("Size at creation: " + arlTest.size());
// Add elements
arlTest.add("D");
arlTest.add("U");
arlTest.add("K");
arlTest.add("E");
// Size after additions
System.out.println("Size after adding: " + arlTest.size());
System.out.println("Elements: " + arlTest);
// Remove by value
arlTest.remove("D");
System.out.println("After removing " + "D": " + arlTest);
// Remove by index
arlTest.remove(2); // removes "K"
System.out.println("After removing index 2: " + arlTest);
// Final size
System.out.println("Final size: " + arlTest.size());
System.out.println("Final list: " + arlTest);
// Contains check
System.out.println("Contains " + "K" + ": " + arlTest.contains("K"));
}
}
Output:
Size at creation: 0 Size after adding: 4 Elements: [D, U, K, E] After removing D: [U, K, E] After removing index 2: [U, K] Final size: 2 Final list: [U, K] Contains K: true
Note: While the example uses single characters, ArrayList can hold any object type—strings, integers, custom classes, and more.

Java
- Java Methods: How to Define, Call, and Use Them Effectively
- Mastering Java ArrayList: Operations, Methods, and Best Practices
- Mastering Java Collection Algorithms: Sorting, Shuffling, and More
- Mastering C# Queues: Enqueue, Dequeue, and First‑In‑First‑Out Operations Explained
- Java ArrayList Explained: Usage, Key Methods, and Practical Examples
- Mastering Java's String compareTo() Method: Syntax, Use Cases, and Practical Examples
- Mastering the 'this' Keyword in Java: Purpose, Usage, and Practical Examples
- Converting a Char to a String in Java – Practical Examples and Best Practices
- Mastering Java Methods: Create, Invoke, and Abstraction
- Java 8 Default Methods Explained: Enhancing Interfaces & Backward Compatibility