What an array is
After this: Read, change and loop over the cells of an array.
On this page · 6 sections · 3 checks
Imagine you are writing a program that stores the marks of five students. You could make one variable for each student.
mark_1 = 72mark_2 = 85mark_3 = 60mark_4 = 91mark_5 = 78int mark1 = 72;int mark2 = 85;int mark3 = 60;int mark4 = 91;int mark5 = 78;This works for five students. Now imagine five hundred. You would have to type five hundred variable names, and you could not write a loop over them, because each one is separate.
An solves this problem. It is a row of boxes that share one name. Each box holds one value, and each box has a position number. From now on we will call each box a .
One name, many cells
Here are the same five marks in one array.
marks = [72, 85, 60, 91, 78]
print(marks[0]) # the first markprint(marks[3]) # the fourth mark
marks[2] = 65 # change the third markprint(marks[2])
print(len(marks)) # how many marks there are72 91 65 5
int[] marks = {72, 85, 60, 91, 78};
System.out.println(marks[0]); // the first markSystem.out.println(marks[3]); // the fourth mark
marks[2] = 65; // change the third markSystem.out.println(marks[2]);
System.out.println(marks.length); // how many marks there are72 91 65 5
Python calls this a list, and Java calls it an array. They are a little different, and a later lesson explains how. For now, you can treat them as the same thing.
The position number of a cell is called its . Here is the array as a row of cells.
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Value | 72 | 85 | 60 | 91 | 78 |
Counting starts at 0
Notice that the first cell has index 0, not index 1. This surprises almost everyone at first.
It helps to think of the index as "how many cells to skip from the start". To reach the first cell you skip none, so its index is 0. To reach the second cell you skip one, so its index is 1.
Five cells have indexes 0 to 4. The last index is always one less than the length.
Watch it happen
Read two cells, then change one. Press the button to run each step.
- Read index 0Read the first cell. Its index is 0.
- Read index 3
- Write 65 at index 2
- 072
- 185
- 260
- 391
- 478
Run an operation to begin.
Going through every cell
A loop can visit every cell in turn. This is the reason arrays beat separate variables: the same few lines work for five marks or for five million.
marks = [72, 85, 60, 91, 78]
total = 0for mark in marks: # takes each mark in turn total = total + mark
print(total)386
int[] marks = {72, 85, 60, 91, 78};
int total = 0;for (int mark : marks) { // takes each mark in turn total = total + mark;}
System.out.println(total);386
Check yourself
marks holds 72, 85, 60, 91, 78 in that order. What is marks[1]?
An array holds 8 values. What is the index of the last value?
What do the two print lines show?
nums = [4, 8, 15]nums[1] = 9print(nums[1])print(len(nums))int[] nums = {4, 8, 15};nums[1] = 9;System.out.println(nums[1]);System.out.println(nums.length);What comes next
You can now use an array. The next lessons look inside it. First we see how the computer reaches any cell so quickly. Then we learn how programmers measure speed. After that we ask what happens when an array needs to grow.
- Say what an array is: a row of cells with one name.
- Read and change a cell by its index, and ask for the length.
- Use the fact that indexes start at 0, so the last index is the length minus one.
- Go through every cell with a loop.