All pages 1 of 8
The array
A row of numbered drawers: you can open any one at once, but a new thing near the front makes the rest move up.
Counted: 5 steps
The steps
- Drawer 0 holds 4, not 16, so open the next one.
- Drawer 1 holds 7, not 16, so open the next one.
- Drawer 2 holds 8, not 16, so open the next one.
- Drawer 3 holds 15, not 16, so open the next one.
- Drawer 4 holds 16: that is it.
- Found 16 in 5 steps.
What it is
Think of a chest of eight drawers with a number on each. The first drawer is number 0, the next is 1, and so on up to 7. Each drawer holds one thing.
In a computer, this is called an array. The things sit side by side in memory, in order, with no gaps. The number on a drawer is called its index.
How a move works
To read drawer 3, you go straight to it. You do not open drawers 0, 1 and 2 first. It takes 1 step, however many drawers there are.
Finding a number is slower. If you do not know where 16 is, you open the drawers one by one from the front. In 4, 7, 8, 15, 16, 23, you meet 16 in the fifth drawer, so it takes 5 steps.
Putting a new thing in the middle is the slowest. Say the drawers hold 4, 8, 15, 16 and 23, and 7 must go in drawer 1. First 23 moves up one drawer, then 16, then 15, then 8. Then 7 is written in. That is 5 steps.
Where you meet it
Arrays are everywhere. A list of high scores, the dots of colour in a photo and the days of a month are all kept in arrays. Most of the other things in this series are built on top of one.
Watch out for
One thing in at the front moves everything behind it. With eight things inside, that is 9 steps: eight moves and a write. Taking the front one out is just as bad. If you add and take at the front a lot, the next page may suit you better.