Codele / Data structures
Arrays that grow
A plain array has a fixed size. A growing list keeps spare room, and when the room runs out it moves to a bigger row of lockers.
- Capacity is how many items fit. Used is how many are in. A growing list keeps some spare room.
- When the row is full, the list makes a new row twice as big and copies every item over.
- Copying costs one move per item, so a copy is slow. Doubling makes copies rare: each one buys a lot of free adds.
- On average an add is still quick. Most adds only fill an empty slot.
- Python lists, JavaScript arrays, Java ArrayList and C++ vector all grow this way, though some add a bit less than double.
Loading the playable lesson…