Lesson 15 of 34 · Arrays & ArrayList
Java ArrayList: Resizable Lists Explained
Arrays have one big limitation: their size is fixed when you create them. A party gains and loses members, an inventory fills up and empties out. For data that grows and shrinks, Java programmers reach for ArrayList — the most used collection class in the whole language.
Creating an ArrayList
ArrayList lives in the java.util package, so it needs an import:
import java.util.ArrayList;
ArrayList<String> party = new ArrayList<>();
party.add("Aria");
party.add("Borin");
party.add("Cass");
System.out.println(party); // [Aria, Borin, Cass]
System.out.println(party.size()); // 3
ArrayList<String> is a generic type: the part in angle brackets says what the list holds, and the compiler rejects anything else. The empty <> on the right (the diamond) repeats the type for you. Unlike arrays, lists print their contents nicely.
Reading and changing elements
party.get(0); // "Aria" (indexes start at 0)
party.set(1, "Bram"); // replace → [Aria, Bram, Cass]
party.add(1, "Dex"); // insert at index 1 → [Aria, Dex, Bram, Cass]
party.remove("Cass"); // remove by value → [Aria, Dex, Bram]
party.remove(0); // remove by index → [Dex, Bram]
party.contains("Bram"); // true
party.indexOf("Zed"); // -1 (not found)
party.isEmpty(); // false
The list shifts its elements automatically when you insert or remove. Asking for an index that does not exist, such as party.get(party.size()), throws an IndexOutOfBoundsException.
Wrapper classes and autoboxing
Generics only work with objects, so ArrayList<int> does not compile. Every primitive type has a wrapper class instead: Integer, Double, Boolean, Character, Long. Java converts between the two automatically — this is called autoboxing and unboxing:
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(42); // the int 42 is boxed into an Integer
int first = numbers.get(0); // and unboxed again
Compare wrapper objects with equals, not ==: two Integer objects holding 1000 are equal but not the same object.
The remove pitfall
remove is overloaded: remove(int index) and remove(Object value). With a list of Integers, an int argument picks the index version:
List<Integer> scores = new ArrayList<>(List.of(10, 20, 30));
scores.remove(1); // removes INDEX 1 → [10, 30]
scores.remove(Integer.valueOf(10)); // removes the VALUE 10 → [30]
remove(Object) returns true if it removed something, which is handy in loops.
List, List.of and iterating
Professional code usually declares variables with the List interface type and creates an ArrayList: List<String> names = new ArrayList<>();. List.of("a", "b") builds a small immutable list — calling add on it throws an UnsupportedOperationException. Wrap it to get a list you can change: new ArrayList<>(List.of("a", "b")).
Loop over a list with for-each, or with an index and get(i). To sort, use Collections.sort(list) from java.util.Collections. Do not remove elements inside a for-each loop over the same list — Java detects it and throws a ConcurrentModificationException.
| Array | ArrayList | |
|---|---|---|
| Size | fixed | grows and shrinks |
| Length | arr.length |
list.size() |
| Read | arr[i] |
list.get(i) |
| Element types | primitives or objects | objects only (Integer, not int) |
Common mistakes
list.length: lists usesize(), with parentheses.ArrayList<int>: use the wrapper typeArrayList<Integer>.- Removing by index when you meant by value (see the pitfall above).
- Comparing Strings with
==: useequals, which compares the text.
Assignment
Write four static methods in Main:
longWords(List<String> words, int minLength)— a new list with the words that have at leastminLengthcharacters, in their original order.countOccurrences(List<String> items, String target)— how many items are equal totarget(compare withequals).evens(int[] numbers)— aList<Integer>with the even numbers of the array, in order.removeAll(List<Integer> numbers, int value)— remove every occurrence of the valuevaluefromnumbers(modifying the list) and return how many were removed.
longWords(List.of("sun", "dragon", "elf", "castle"), 4) // [dragon, castle]
countOccurrences(List.of("gem", "sword", "gem"), "gem") // 2
evens(new int[]{3, 8, 10, 7}) // [8, 10]
List<Integer> numbers = new ArrayList<>(List.of(10, 1, 20, 1));
removeAll(numbers, 1) // 2, numbers is now [10, 20]Premium lesson
Unlock the full Learn Java course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Lists that grow in other languages
Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.
Lesson last updated on .