GCSE Link: 2.05 (Arrays)
An array is a static data structure which stores values of the same data type under a single identifier.
The values can then be retrieved using their index in the array, an integer in the range
[0 .. arr.Length-1].
If the array consists of values of basic data types (such as integers, strings, etc.), it is called a one-dimensional array. A multi-dimensional array contains arrays inside of it.
"Static" means that its length is fixed at compile time. The opposite of this would be
"dynamic", where the size can change at runtime.
Table 1 shows a comparison of static and dynamic data structures.
Table 1
| Static Data Structures | Dynamic Data Structures | |
|---|---|---|
| Length | Fixed at compile time | Can change throughout runtime |
| Stored... | In consecutive memory locations (making them quicker to read at once) | Wherever there is space (dynamically allocated from the heap) |
| Pointers used? | No (so less space is required) | Yes (to keep track of where all the items are) |
| Potentially wasted space? | Yes (because each value will have a fixed maximum size in memory) | No (because they only take up however much space they need) |
If a 2D array,
arr, is used to store a game board, how would you retrieve the value in the
third row and fifth column?
arr[2][5] (Python) or
arr[2, 5] (C#)