A variable is a memory location that can store a value. It can be thought of as a box in which values are stored. The value held in the box can change, or vary. But each variable can only hold one item of data.
An array is a series of memory locations – or ‘boxes’ – each of which holds a single item of data, but with each box sharing the same name. All data in an array must be of the same data type.
For example, imagine that a score table in a game needs to record ten scores. One way to do this is to have a variable for each score:
score_0 score_1 score_2 score_3 score_4 score_5 score_6 score_7 score_8 score_9
This would work, but there is a better way. It is much simpler to keep all the related data under one name. We do this by using an array.
Instead of having ten variables, each holding a score, there could be one array that holds all the related data:
By using this array, all 10 data items can be stored in one place.
It helps to think of an array as a row of cells, like the ones found in a table. Each cell represents an element:
The individual values, or array elements, are numbered 0 to 9 because computers start counting at 0.
Arrays are named like variables. The number in brackets determines how many data items the array can hold. The array score(9) would allow ten data items to be stored.
Any facility that holds more than one item of data is known as a data structure. Therefore, an array is a data structure.