Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 04 / Topic 4 of 10

Why arrays are not enough: meet ArrayList

An array's size is fixed forever. But a real shopping cart doesn't know in advance how many items Asha will put in it — it grows one scan at a time. Java's answer is ArrayList<T>, a class from java.util that behaves like a resizable array: add to it, remove from it, ask its current size, any time.

Array (int[], String[], ...) ArrayList<T>
Size fixed at creation, never changes grows and shrinks as you add/remove
Holds primitives (int, double, ...) or objects objects only — ArrayList<int> does not compile
Create new int[5] or {1, 2, 3} new ArrayList<>()
Add an item not possible — only arr[i] = value on an existing slot list.add(value)
Read the length arr.length (a field) list.size() (a method)

The "objects only" row matters: you cannot write ArrayList<int>. You write ArrayList<Integer> — Integer is the object form of int. Section 6 explains how int values get in and out of an Integer-based list without you writing any conversion code.

Done here? The practice set for Part 4 — including the solutions — is on the part page.

Search the guide