Arrays
An array is defined as a finite, ordered set of elementary data types.
Examples of elementary data types include integers, real numbers, characters, and Boolean.
Many languages have built-in structured data types such as a string, array or list.
Arrays are designed to store multiple elements e.g.
car_makers = ['ford', 'jaguar', 'Nissan', 'Mazda']
Elements are accessed by referencing their location within the array, e.g.
- car_makers[0] would return ford
- car_makers[2] would return Nissan
Note that it is common for the first element of an array to be given an index of 0 rather than 1,
Two dimensional arrays are a little more complex but they are really an array of arrays. The best way to understand a 2-dimensional array is to think of it as a way of storing and accessing information within a grid made up of rows and columns. You may have used this method to define a cell in a spreadsheet, where you give the location by row and column.
Imagine a 2-dimensional array called "numbers" with 3 rows and 4 columns.
Column 0 | Column 1 | Column 2 | Column 3 | |
Row 0 | 1 | 2 | 3 | 4 |
Row 1 | 5 | 6 | 7 | 8 |
Row 2 | 9 | 10 | 11 | 12 |
Elements in the array can be referred to by their row and column number, so that numbers[1][3] = 8