This article is unfinished. Please consider joining and adding to this article. Read about Page layout beforehand.
- An array is an ordered sequence of simple data types, all of the same type
- An array is used to store a list of items - a variable only stores one item.
- Each item in the list is given an index number. Some languages index from 0 onwards, some index from 1. Haggis Pseudocode (which is used by the SQA in exams) is indexed from 0 onwards.
- Arrays are a good way of storing lists as they can be processed using their index, making it easy to search and sort the list
An array is an ordered sequence of simple data, such as numbers or strings. Every item in the array has an index number.
The easiest way to think about this is to imagine a series of boxes. The boxes represent each memory location. The boxes are numbered. Each number is the box's index.
In this example, the numbers are indexed from zero. Computers and programming languages like starting with zero instead of one, because to miss zero wastes data (for example, in graphics, black is usually represented by the number zero!).
In the code below, the array (which has been called numbers) is filled with values. Try the exercises to further understand arrays.
After the first set of lines, the array would look like this:
Arrays can only be accessed using the index number.