Two ways to hold a row of values in Java: the raw array you size once, and the ArrayList that grows for you — each with a different bill.
1 The raw array
int[] a = new int[5]; — fixed length forever, exposed via a.length. Contiguous memory makes a[i] a single jump: O(1).
2 The ArrayList
From java.util.ArrayList: wraps an array it secretly doubles when full. size() tells the truth; capacity stays hidden.
3 Amortized add
Most adds are one O(1) write. Every so often the list grows and copies everything — spread across all adds, still O(1) average.
4 The costly ends
add(0, x) or remove(0) shift every element. Ends are cheap; middles and fronts are not.
5 2-D arrays
int[][] grid = new int[3][4]; — grid.length is the row count, grid[0].length the columns. Rows can even have different lengths.
Arrays vs ArrayList
The raw array1int[] a = new int[5]; — fixed length forever, exposed via a.length. Contiguous memory makes a[i] a single jump: O(1). The ArrayList2From java.util.ArrayList: wraps an array it secretly doubles when full. size() tells the truth; capacity stays hidden. Amortized add3Most adds are one O(1) write. Every so often the list grows and copies everything — spread across all adds, still O(1) average. The costly ends4add(0, x) or remove(0) shift every element. Ends are cheap; middles and fronts are not. 2-D arrays5int[][] grid = new int[3][4]; — grid.length is the row count, grid[0].length the columns. Rows can even have different lengths. 5 building blocks make up Arrays vs ArrayList
The ArrayList’s secret backing array: add until it’s full, then watch the grow-and-copy moment.
1 / 10Behind every ArrayList sits a plain array. This one: size 4, capacity 4 — completely full.
Every add asks one question: is there room? The answer decides between a single write and a full copy.
process decision input / output start / end An array scan for max and average, then an ArrayList doing indexed surgery.
maintemps holds 5 readings — a.length is 5, fixed forever.
Memory
temps= [21, 34, 29, 35, 26]
Console
output appears as the program runs…
An int[] is one contiguous block — slot i sits at base + i × 4 bytes. That is why get is a single jump.
Drag to orbit the scene. Press Play to watch memory in action.
remove(int) vs remove(Integer) — the most-tripped Java trap on one screen.
Your predicted output — one line per console line
Hint: remove(1) removes INDEX 1. remove(Integer.valueOf(30)) removes the VALUE 30. And get(0) reads whatever survived.
Click a term, then click its meaning to lock the pair.
8 conceptsTap a card to expand ?arr.lengthdrops into place
?new ArrayList<>()drops into place
?get(i)drops into place
?add(x)drops into place
?add(0, x)drops into place
?remove(Integer.valueOf(x))drops into place
?grid.lengthdrops into place
?for (int t : arr)drops into place
01Arrays are fixed-length and lightning-fast; ArrayList trades a little overhead for growth.
02a.length is a field (no parentheses); list.size() is a method — mixing them up is a compile error, thankfully.
03Growth = allocate 2× + copy everything, but amortized add stays O(1).
04Ends are cheap, middles shift: add(0, x) and remove(0) cost O(n).
05remove(1) is by index; remove(Integer.valueOf(1)) is by value — memorize both forms.
06Enhanced for walks any array or collection cleanly; use indexed for when you need the index.