โก Key Constraint:O(1) access but fixed capacity (O(n) grow/copy) + O(n) middle insert โ mirror of a LinkList.
๐ Core
1. The Array (Contiguous, O(1) Access)
Structure โ fixed-size contiguous block of equal-sized slots โ element kโs address is arithmetic โน O(1) access.
ArrayR โ FIT1008โs fixed-size referential array (references to any object); slots start as shared None.
Boundary โ cannot be size 0 (hence ArrayStack.MIN_CAPACITY = 1).
2. Address Arithmetic & Locality
Formula โ addr(A[k])=base+kโ slot_size โ one multiply-add, no scan.
Cache locality โ a fetch loads a whole cache line (~64 B) โน sequential access prefetches neighbours.
Multidim โ row-majoraddr(A[i][j])=base+(iโ cols+j)โ slot_size; column-major of a row-major array thrashes the cache.
โ๏ธ Core Implementation
๐น ArrayR โ fixed-size referential array
access / set / capacity
from referential_array import ArrayRarray = ArrayR(4) # fixed size 4, slots = Nonearray[3] = value # SET โ O(1)element = array[2] # ACCESS โ O(1): base + 2*slot_sizelength = len(array) # capacity โ O(1) (stored)
๐ก Common Mistake:Construction is O(n), access is O(1) โ ArrayR(n) initialises all n slots (linear), but access computes one address; algorithms amortise the allocation over many constant-time accesses.
When It Flips:O(1) random access + cache-friendliness vs fixed capacity (growth = full copy) and O(n) structural edits โ the mirror image of a LinkList. ADTs built on it: ArrayStack, ArrayList, Hash Table, Heap.
๐ Exam Execution Trace
Applied Exercise
Problem: Derive the 1-D and 2-D (row-major) address formulas.
Derivation Proof / Hand-Calculation Walkthrough: