Arrays: everything in one row
An array stores its elements side by side in one block of memory. Every element has the same size, so the computer finds element i with one calculation: start address plus i times the element size. That is why reading any element by index takes the same short time, O(1), whether the array holds ten items or ten million.
The price is rigidity. A classic array has a fixed size chosen when it is created, and it is only fast when you know the index. Looking for a value without knowing where it is means checking elements one by one: O(n).
Hourly temperatures for a day, the pixels of one row of an image, the 64 squares of a chess board: fixed-size data you read by position.
In code: Python · Rust
temps = [21, 19, 23, 25] # a list: Python has no fixed-size array in the core language
temps[2] # 23, O(1)
from array import array
prices = array('d', [9.5, 10.0, 10.25]) # compact array of floatslet temps: [i32; 4] = [21, 19, 23, 25]; // size is part of the type
let third = temps[2]; // 23
// temps[10] would panic at runtime: indexes are bounds-checked- Python lists store references to objects; array.array and NumPy store the raw values side by side.
- In Rust the length of a fixed array is part of its type: [i32; 4] and [i32; 5] are different types.