2.2.1 One-Dimensional Arrays

Overview

A one-dimensional array stores several values of the same type under one name. An airline application might keep seat numbers in an `int[]`. Each position has an index beginning at zero; the array length tells how many positions exist.

int[] seatNumbers = { 12, 14, 16 };
int firstSeat = seatNumbers[0];
One Indexed Sequence
Object diagram for one-dimensional arrays: one indexed sequence.
One Indexed Sequence
  • New
  • Changed
  • Removed

The array holds three integers. Index 0 contains 12, index 1 contains 14, and index 2 contains 16. `firstSeat` receives 12. The array has a fixed length after creation; replacing an element does not add another position.

Write the three index-value pairs in a small table. A later loop can traverse those positions in order; first make sure you can read a single element by its index.

Trace the Outcome

Predict the result of seatNumbers[1] and identify why seatNumbers[3] is invalid. Distinguish an index from the value stored there.