Arrays (OCR GCSE Computer Science): Revision Notes
📚 Revision Notes
Arrays
An array is a data structure used to store multiple values of the same data type in a single variable. Arrays are particularly useful when solving problems that require the storage of large amounts of related data, such as lists of names or scores.
Key Characteristics of Arrays
- Arrays have a fixed length or static structure, meaning the size of the array is defined when it is created and cannot be changed.
- Arrays can be one-dimensional (1D) or two-dimensional (2D). A 1D array is like a list, while a 2D array is like a grid or table with rows and columns.
- 2D arrays can be used to emulate database tables, where each row is a record and each column is a field. This is useful for storing structured data like spreadsheets.
One-Dimensional Arrays (1D)
A 1D array stores a simple list of values, accessed using an index starting from 0.
| Operation | Example |
|---|---|
| Creating a 1D Array | names = ["Bob", "Alex", "Chris"] |
| Accessing an Item | names[1] → Output: Alex (index starts at 0) |
| Looping through a 1D Array | for name in names:print(name) |
| Length of a 1D Array | len(names) → Output: 3 |
| Summing Items in an Array | scores = [5, 12, 5, 7, 8] total = sum(scores) → Output: 37 |
Two-Dimensional Arrays (2D)
A 2D array stores data in a grid, allowing access using two indices: one for the row and one for the column. It's useful for working with more complex data like tables.
| Operation | Example |
|---|---|
| Creating a 2D Array | matrix = [[1, 2], [3, 4], [5, 6]] (3 rows and 2 columns) |
| Accessing an Item | matrix[1][1] → Output: 4 (Row 1, Column 1) |
| Looping through a 2D Array | for row in matrix:for col in row:print(col) |
| Emulating a Table | customers = [["John", "London"], ["Jane", "Paris"]] |
| Length of a 2D Array | len(matrix) → Output: 3 (number of rows) |
infoNote
Key Points to Remember
- Arrays have a fixed length, meaning the size is set when they are created.
- 1D arrays are used for storing a list of data, while 2D arrays can store more complex data in a table-like structure.
- 2D arrays can be used to emulate database tables, representing collections of fields and records.