An ArrayList stores elements in order and can grow or shrink as you add or remove items.
In this example, you will create a list of programming languages, add elements, and remove them by value and by index.
Source code
Save the following code as ArrayListExample.java:
import java.util.ArrayList;
import java.util.List;
public class ArrayListExample {
void main() {
ArrayList<String> languages = new ArrayList<>(
List.of("Java", "Python"));
IO.println("Initial list: " + languages);
languages.add("JavaScript");
IO.println("After adding JavaScript: " + languages);
languages.add(1, "Kotlin");
IO.println("After inserting Kotlin: " + languages);
boolean removed = languages.remove("Python");
IO.println("Python removed: " + removed);
IO.println("After removing Python: " + languages);
String removedLanguage = languages.remove(0);
IO.println("Removed at index 0: " + removedLanguage);
IO.println("Final list: " + languages);
IO.println("Total languages: " + languages.size());
}
}
Explanation
The following statement creates a mutable list containing two strings:
ArrayList<String> languages = new ArrayList<>(List.of("Java", "Python"));
List.of() creates an unmodifiable list. Passing it to the ArrayList constructor copies its elements into a new list that supports adding and removing items.
The type argument <String> restricts this list to strings. The diamond operator <> lets the compiler infer the constructor’s type argument.
Adding an item
Use add(element) to append an item to the end:
languages.add("JavaScript");
The list becomes:
[Java, Python, JavaScript]
To insert an item at a specific position, use add(index, element):
languages.add(1, "Kotlin");
Indexes start at zero. This inserts "Kotlin" at the second position and shifts the following elements to the right.
Removing an item by value
Use remove(Object) to remove the first matching item:
boolean removed = languages.remove("Python");
This method returns true if an item was removed, or false if no matching item was found.
If the list contains duplicates, only the first matching occurrence is removed.
Removing an item by index
Use remove(int) to remove an item at a specific position:
String removedLanguage = languages.remove(0);
This removes the first item and returns it. The remaining elements shift to the left.
The index must refer to an existing element. Otherwise, the method throws an IndexOutOfBoundsException.
Getting the number of items
Use size() to read the number of elements:
languages.size()
Unlike an array’s length field, an ArrayList uses a method with parentheses.
Run the example
Compile and run the file:
javac ArrayListExample.java
java ArrayListExample
Output
Initial list: [Java, Python]
After adding JavaScript: [Java, Python, JavaScript]
After inserting Kotlin: [Java, Kotlin, Python, JavaScript]
Python removed: true
After removing Python: [Java, Kotlin, JavaScript]
Removed at index 0: Java
Final list: [Kotlin, JavaScript]
Total languages: 2
Removing integers by value
For an ArrayList<Integer>, passing an int to remove() selects the index-based overload:
numbers.remove(1);
This removes the element at index 1. To remove the value 1, explicitly pass an Integer:
numbers.remove(Integer.valueOf(1));
