Use the technical terms associated with arrays

10.2 Arrays 📚

Arrays are like a row of lockers where each locker holds a single value. They are a fundamental data structure in computer science that lets us store a collection of items of the same type in a single variable.

Key Technical Terms

Term Definition
Array A collection of elements stored in contiguous memory locations.
Element A single value stored in an array, accessed by its index.
Index The position of an element in the array, usually starting at 0.
Size / Length Number of elements an array can hold. In many languages, array.length gives this value.
Bounds The valid range of indices: 0 to size-1 for zero‑based arrays.
Zero‑based Indexing Indices start at 0 (e.g., A[0] is the first element).
One‑based Indexing Indices start at 1 (common in mathematics).
Contiguous Memory Array elements are stored next to each other in RAM, which makes access fast.
Static Array Size is fixed at compile time.
Dynamic Array Size can change at runtime (e.g., ArrayList in Java).
Multi‑dimensional Array An array of arrays, like a grid or table.
Jagged Array Rows of different lengths.
Slice / Subarray A contiguous portion of an array.
Index Out of Bounds Attempting to access an index outside the valid range.

Mathematical view of an array:

$$A : \{0,1,\dots,n-1\} \to \text{Type}$$

For example, if A[0] = 10, then the first element is 10.

Creating and Using Arrays

  1. Declare an array: int[] scores = new int[5]; – creates space for 5 integers.
  2. Assign values: scores[0] = 88; scores[1] = 92;
  3. Read values: int first = scores[0];
  4. Check size: int n = scores.length;

Example: Locker Analogy 🚪

Imagine a row of 5 lockers labelled 0 to 4. Each locker can hold one book (an element). The whole row is the array books[5]. If you want the third book, you look at locker 2 (zero‑based).

Multi‑dimensional Arrays: The Spreadsheet 📊

A 2‑D array is like a spreadsheet with rows and columns.

Row Column Value
0 0 10
0 1 20
1 0 30
1 1 40

Access the element in the second row, first column: matrix[1][0] → 30.

Common Array Operations

  • Get element: array[i]
  • Set element: array[i] = value;
  • Find length: array.length
  • Loop through: for (int i = 0; i < array.length; i++) { … }
  • Reverse: swap first and last, second and second‑last, etc.

Pitfalls to Avoid ⚠️

  • Index Out of Bounds: array[5] in a 5‑element array causes an error.
  • Off‑by‑one errors: forgetting that the last valid index is size-1.
  • Assuming arrays are always dynamic – many languages require a fixed size.

Quick Quiz

  1. What is the index of the first element in a zero‑based array?
  2. How many elements can you access in an array of size 10?
  3. Write the code to create a 3‑row, 4‑column integer matrix.

Answer Key (for teachers):

  • Index 0.
  • 10 elements (indices 0–9).
  • int[][] matrix = new int[3][4];

Summary

Arrays give you a fast, organised way to store and retrieve data. Remember the key terms: array, element, index, size, bounds, zero‑based indexing, static vs dynamic, multi‑dimensional, jagged, slice, and out‑of‑bounds errors. Mastering these will help you tackle more advanced data structures later.

Revision

Log in to practice.

2 views 0 suggestions