ArrayList in Java
ArrayList in Java is a class that implements the List interface using a resizable array. It is used to store an ordered group of elements whose number can change while the program runs. Unlike a normal array, an ArrayList grows automatically when you add elements. It is the most commonly used collection in Java. In this lesson, you will learn how to create an ArrayList and perform all the common operations on it.
Declaration
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, SerializableArrayList is in the java.util package. RandomAccess is a marker interface meaning "getting an element by index is fast".
Features of ArrayList
- Dynamic size: it grows automatically as elements are added.
- Maintains insertion order: elements stay in the order they were added.
- Allows duplicates and null: any value can appear more than once.
- Fast access by index:
get(i)takes the same time whatever the size. - Not synchronized: it isn't safe to change from several threads at once without extra care.
Constructors
| Constructor | Description |
|---|---|
ArrayList() | It creates an empty list. |
ArrayList(int initialCapacity) | It creates an empty list with room for the given number of elements. |
ArrayList(Collection<? extends E> c) | It creates a list containing the elements of another collection. |
Example: Common ArrayList Operations
Let's see an example that adds, reads, updates, removes and searches.
import java.util.ArrayList;
import java.util.List;
public class Marks {
public static void main(String[] args) {
List<String> students = new ArrayList<>();
students.add("Navika");
students.add("Srihitha");
students.add("Advitha");
System.out.println("Students: " + students);
System.out.println("First: " + students.get(0));
students.set(1, "Srihitha A");
students.remove("Advitha");
System.out.println("After changes: " + students);
System.out.println("Size: " + students.size());
System.out.println("Has Navika? " + students.contains("Navika"));
}
}Students: [Navika, Srihitha, Advitha]
First: Navika
After changes: [Navika, Srihitha A]
Size: 2
Has Navika? trueExplanation: add appends each name to the end. get(0) reads the first element. set(1, ...) replaces the second element, and remove("Advitha") deletes that name. size() and contains report the list's state.
Iterating Over an ArrayList
There are several ways to visit every element:
import java.util.ArrayList;
import java.util.List;
public class Loops {
public static void main(String[] args) {
List<Integer> marks = new ArrayList<>(List.of(91, 88, 95));
for (int i = 0; i < marks.size(); i++) {
System.out.print(marks.get(i) + " ");
}
System.out.println("(index loop)");
for (int m : marks) {
System.out.print(m + " ");
}
System.out.println("(for-each)");
marks.forEach(m -> System.out.print(m + " "));
System.out.println("(forEach)");
}
}91 88 95 (index loop)
91 88 95 (for-each)
91 88 95 (forEach)Explanation: The index loop is useful when you need the position. The for-each loop is the simplest. forEach takes a lambda and runs it for each element.
Sorting an ArrayList
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortList {
public static void main(String[] args) {
List<String> names = new ArrayList<>(
List.of("Srihitha", "Navika", "Advitha"));
Collections.sort(names);
System.out.println(names);
names.sort(Comparator.reverseOrder());
System.out.println(names);
}
}[Advitha, Navika, Srihitha]
[Srihitha, Navika, Advitha]Explanation: Collections.sort sorts in natural (alphabetical) order. list.sort(Comparator.reverseOrder()) sorts in reverse order. Sorting is covered fully in "Sorting Collections in Java".
Common Mistakes
Using int as the type argument. Write ArrayList<Integer>, not ArrayList<int>.
Removing elements inside a for-each loop. Use removeIf or an iterator (see "ConcurrentModificationException in Java").
Expecting ArrayList to be thread-safe. Use a concurrent collection when several threads change it.
Interview Corner
What is an ArrayList in Java?
ArrayList is a resizable-array implementation of the List interface in java.util. It keeps insertion order, allows duplicates and null, provides fast random access by index, and grows automatically when elements are added. It isn't synchronized.
What is the default capacity of an ArrayList?
An ArrayList created with new ArrayList<>() starts with an empty internal array and allocates a capacity of 10 when the first element is added. After that, it grows by about 50% each time it fills up.
Practice
- Easy: Create an
ArrayListof five prices, print the total and the highest price. - Harder: Read names until the user types "done", store them in an
ArrayList, remove duplicates while keeping the first occurrence, and print them sorted.
Key Takeaways
Was this tutorial helpful?