A List That Resizes Itself
A plain Java array has a fixed length: decide on ten slots and you have ten slots forever. ArrayList is the resizable alternative from java.util - it grows as you add and shrinks as you remove, and it carries handy methods for the things you actually do with a list.
Note the type in angle brackets - List<String> - which tells the compiler this list holds String values. The <> (the "diamond") on the right lets Java infer the same type without repeating it. And note that you import java.util.ArrayList; it is not available by default.
Declare as List, Construct as ArrayList
You will almost always see the variable typed as the List interface rather than the concrete ArrayList:
List<String> names = new ArrayList<>();
This is good habit, not a rule. Coding to the List interface means the rest of your code does not care which list implementation you used, so you can swap in a different one later without touching it. For everyday use the two behave the same.
Adding, Getting, and Setting
add(value)appends to the end.add(index, value)inserts at a position, shifting later elements right.get(index)reads the element at a position (zero-based).set(index, value)overwrites an existing element.
Indices are zero-based, and get on an out-of-range index throws IndexOutOfBoundsException.
Removing Elements
There is a classic trap when the list holds Integer. remove(int) is "remove by index" and remove(Object) is "remove by value", so:
List<Integer> nums = new ArrayList<>(List.of(10, 20, 30));
nums.remove(1); // removes index 1 -> the value 20
nums.remove(Integer.valueOf(20)); // removes the value 20
Wrap the value in Integer.valueOf(...) when you mean "remove this value", not "remove this index".
Size, Contains, and Index Lookups
List.of(...) builds a quick immutable list; passing it to the ArrayList constructor gives you a mutable copy seeded with those values.
Looping Over an ArrayList
The cleanest loop is the enhanced for (the for-each loop):
When you need the index too, use a counted loop with size() and get(i):
One rule: do not add or remove elements from a list while a for-each loop is iterating it - that throws ConcurrentModificationException. To delete matching items safely, use removeIf:
Sorting
Collections.sort orders a list in place using natural ordering (alphabetical for strings, numeric for numbers):
For custom orderings, pass a Comparator to list.sort(...) - for example names.sort(Comparator.comparingInt(String::length)) to sort by length.
ArrayList Holds Objects, Not Primitives
You cannot write ArrayList<int>. Generics work with object types only, so use the wrapper class Integer, Double, Boolean, and so on:
Java's autoboxing converts between int and Integer for you, so this reads naturally - just remember the list itself stores Integer objects.
Next: HashMap
ArrayList is the right tool when you care about order and position. When you need to look things up by a key - a username to a user, a product code to a price - you want a HashMap, which is the next page.
Frequently Asked Questions
How do you create an ArrayList in Java?
Declare it with the element type in angle brackets and call the constructor: ArrayList<String> names = new ArrayList<>();. The <> on the right (the diamond) lets Java infer the type. You usually type the variable as the List interface: List<String> names = new ArrayList<>();.
What is the difference between an array and an ArrayList in Java?
A plain array has a fixed length set when you create it and can hold primitives like int. An ArrayList grows and shrinks automatically as you add and remove elements, holds objects only (so int becomes Integer), and comes with methods like add, remove, contains, and size. Use an array for fixed-size primitive data; use an ArrayList when the size changes.
How do you remove an element from an ArrayList?
Call remove(index) to remove by position, or remove(object) to remove the first matching element. Be careful with Integer lists: list.remove(2) removes index 2, while list.remove(Integer.valueOf(2)) removes the value 2. To remove while looping, use an Iterator's remove() or removeIf(...) to avoid a ConcurrentModificationException.