Ten structures that most programs are built from, each drawn the way you would sketch it on a whiteboard: what it looks like inside, what every operation costs, and when to reach for it. Each card ends with the Java class to use.
I need to…
Start from what you need to do, and jump to the structure that does it cheaply.
Green is constant time, amber logarithmic, red linear in the number of items. Blue grows with something else: the length of a word or the number of neighbors. * means on average over many calls.
Array
A fixed row of equal sized slots in one block of memory. The position of any slot is a sum, so reaching it takes one step.
read or write a[i]O(1)find a valueO(n)insert or delete in the middleO(n)
Reach for it when
You know the size, or the values are small numbers you can use as the index
Counting: letters, digits, buckets
Grids, matrices and DP tables
In Java:int[], char[], boolean[]. See int[] counts.
Dynamic array
An array that grows: it keeps spare room at the end, and when that runs out it copies everything into a bigger array.
get(i), set(i)O(1)add at the endO(1)*insert or remove in the middleO(n)
Reach for it when
The default list: build a result, then read it by index
Adjacency lists for graphs
A backtracking path: add, recurse, remove the last
Going further: the Java Collections Field Guide has the same sketches next to each Java class, with what each one costs and the mistake people make with it.