Skip to main content
SingaporeComputer ScienceSyllabus dot point

How are arrays and records stored, and why is array access so fast?

Describe arrays and records as data structures, explain constant-time array indexing and contrast static with dynamic arrays

A focused answer to the H2 Computing outcome on arrays and records. Contiguous storage and constant-time indexing, one and two dimensional arrays, records as fields of mixed type, and static versus dynamic arrays.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to describe arrays and records as data structures, explain why array indexing is constant time, and contrast static with dynamic arrays. The central idea is that an array's contiguous layout lets any element be reached by a single address calculation, while a record bundles related values of different types into one named unit.

The answer

Arrays and contiguous storage

An array is an ordered collection of elements of the same type, stored in contiguous (adjacent) memory locations and accessed by an integer index starting at 0. Because the elements sit back to back, the address of element ii is:

address(i)=base+i×elementSize\text{address}(i) = \text{base} + i \times \text{elementSize}

This single multiplication and addition is independent of the array's length, so reading or writing array[i] is O(1)O(1) - constant-time random access. This direct addressing is the array's defining strength.

The cost of insertion and deletion

The same fixed layout makes structural changes expensive. Inserting at the front, or deleting from the front, requires shifting every following element, which is O(n)O(n). Only appending at the end (with room) is cheap.

Two-dimensional arrays

A 2D array (a grid or matrix) is indexed by row and column, grid[r][c], and is typically stored row by row in memory (row-major order). The address arithmetic extends naturally, so element access is still O(1)O(1).

Records

A record (or struct) groups several fields, which may be of different types, under one name:

# a record-like structure for one student
student = {"name": "Wei Ling", "age": 17, "grade": "A"}

Where an array holds many items of one type, a record holds related items of mixed types as one logical unit. An array of records then stores many such units, indexed by position - far safer than keeping several parallel arrays in step.

Static versus dynamic arrays

  • A static array has a fixed size, set at creation; it cannot grow.
  • A dynamic array can resize: when full, it allocates a larger block and copies the elements, giving amortised O(1)O(1) appends despite occasional costly copies. Python's list is a dynamic array.

Examples in context

Example 1. An image as a 2D array. A greyscale image is a grid of pixel values stored as a two-dimensional array. Reading pixel (r, c) is instant address arithmetic, which is why image filters that touch every pixel are efficient - each access is O(1)O(1) and the whole pass is O(width×height)O(\text{width} \times \text{height}).

Example 2. A record of fixed format. A weather station logs each reading as a record of timestamp, temperature, humidity and pressure. Storing readings as an array of these records keeps every reading's fields together and lets the program jump to the thousandth reading instantly by index, rather than juggling four separate arrays.

Try this

Q1. State and justify the time complexity of reading array[100] in an array of 1000 elements. [2 marks]

  • Cue. O(1)O(1) - the address is computed directly from base, index and element size, regardless of length.

Q2. Why is inserting an element at the front of an array O(n)O(n)? [1 mark]

  • Cue. Every existing element must shift one place right to make room, which is nn moves.

Q3. Give one advantage of an array of records over several parallel arrays. [1 mark]

  • Cue. Each item's fields stay bundled together, so the arrays cannot fall out of step and one record moves as a unit.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original5 marks(a) Explain why accessing an element of an array by its index is an O(1)O(1) operation. (b) State the time complexity of inserting a new element at the start of an array of nn elements, and explain why. (c) Distinguish between a static and a dynamic array.
Show worked answer →

(a) An array stores its elements in contiguous memory. The address of element ii is computed directly as textbase+itimestextelementSize\\text{base} + i \\times \\text{elementSize}, a single arithmetic step independent of nn, so indexing is O(1)O(1).

(b) Inserting at the start is O(n)O(n): every existing element must be shifted one place to the right to make room, which is nn moves.

(c) A static array has a fixed size set when it is created and cannot grow. A dynamic array can grow (and shrink); when it fills, it allocates a larger block and copies the elements across, giving amortised O(1)O(1) appends.

Markers reward the address-arithmetic reason for O(1)O(1) indexing, the shifting that makes front insertion O(n)O(n), and the fixed-versus-resizable distinction.

Original4 marksA program stores student data where each student has a name (text), an age (integer) and a grade (single character). (a) Explain why a record is a suitable structure for one student. (b) Explain how an array of records would store many students, and give one advantage over separate parallel arrays.
Show worked answer →

(a) A record groups several fields of different types under one name, so a single Student record holds name (string), age (integer) and grade (char) together as one logical unit. This keeps related data of mixed type bundled, rather than scattered.

(b) An array of records stores many students as a sequence of Student records, indexed by position - students[3] is the fourth student, and students[3].age is their age. An advantage over three separate parallel arrays (names[], ages[], grades[]) is that each student's data stays together, so there is no risk of the arrays falling out of step, and adding or moving a student moves one record rather than synchronising several arrays.

Markers reward the record as a named group of mixed-type fields, the array-of-records indexing, and the cohesion/consistency advantage over parallel arrays.

Related dot points