How the computer finds a cell
After this: Say why reading the last cell takes the same work as reading the first.
On this page · 6 sections · 3 checks
An array can hold 10,000 values. Suppose you ask for the last one, at index 9,999. Does the computer walk past the other 9,999 cells to get there?
It does not. It goes straight there, and this takes the same time as reading the first cell. This lesson shows how.
Memory is a long row of numbered places
A running program keeps its data in the computer's . You can picture memory as one very long row of numbered places, like houses along a road. The number of a place is called its . If the computer knows an address, it can go there directly.
When you create an array, the computer sets aside a block of places that sit side by side, with no gaps. It also remembers one number: the address where the block starts.
Finding a cell with one calculation
Every cell in an array has the same width. Here is an array of five numbers. It starts at address 1000, and each cell is 4 places wide.
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Address | 1000 | 1004 | 1008 | 1012 | 1016 |
| Value | 10 | 20 | 30 | 40 | 50 |
To reach index 3, the computer does not look at indexes 0, 1 and 2. It calculates the address:
- Multiply the index by the width: 3 × 4 = 12.
- Add that to the start: 1000 + 12 = 1012.
So the rule is address = start + index × width.
Now try index 9,999 in a much longer array: 9,999 × 4 = 39,996, and 1000 + 39,996 = 40,996. The numbers are bigger, but the work is the same: one multiplication and one addition. No cell is visited on the way.
This rule also explains why counting starts at 0. The first cell is at the start of the block, and start + 0 × width is the start.
nums = [10, 20, 30, 40, 50]print(nums[2]) # start + 2 × cell widthprint(nums[-1]) # the last one, counted from the end30 50
int[] nums = {10, 20, 30, 40, 50};System.out.println(nums[2]); // start + 2 × cell widthSystem.out.println(nums[nums.length - 1]); // the last one30 50
Watch it happen
Here are three reads. Count the steps each one takes.
- Read index 0Read the first cell. Count the steps.
- Read index 5
- Read index 3
- 010
- 120
- 230
- 340
- 450
- 560
Run an operation to begin.
Past the end
Five cells have indexes 0 to 4. What happens if you ask for index 5?
The calculation would still give an address, but that place in memory is not part of your array. Something else may be stored there. So Python and Java both check the index first, and they stop with an error instead of reading that place.
nums = [10, 20, 30, 40, 50]print(nums[5])# IndexError: list index out of rangeint[] nums = {10, 20, 30, 40, 50};System.out.println(nums[5]);// ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5Check yourself
An array holds 10,000 numbers. Why does reading index 9,999 take the same work as reading index 0?
What does this print?
nums = [10, 20, 30, 40, 50]print(nums[2]) # start + 2 × cell widthprint(nums[-1]) # the last one, counted from the endint[] nums = {10, 20, 30, 40, 50};System.out.println(nums[2]); // start + 2 × cell widthSystem.out.println(nums[nums.length - 1]); // the last onenums holds five numbers. What does nums[5] do?
Connect
- Picture memory as a long row of numbered places, each with an address.
- Calculate where a cell is: start plus index times width.
- Say why reading index 9,999 takes the same work as reading index 0.
- Say what happens when an index is past the end, in Python and in Java.