Array (Data Structure)

Context: FIT1008_MOC ยท the contiguous archetype of a Data Structure ยท the storage behind ArrayStack, hash tables, heaps, dynamic lists

Quick Revision

  • ๐ŸŽฏ Objective: fixed-size contiguous block of equal-sized slots โž” element โ€˜s address is pure arithmetic, giving access.
  • ๐Ÿ“ฆ Core Components: address arithmetic โž” cache locality โž” row-major multidim layout.
  • โšก Key Constraint: access but fixed capacity ( grow/copy) + middle insert โ€” mirror of a LinkList.

๐Ÿ“ Core

1. The Array (Contiguous, Access)

  • Structure โž” fixed-size contiguous block of equal-sized slots โž” element โ€˜s address is arithmetic โŸน 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 โž” โ€” one multiply-add, no scan.
  • Cache locality โž” a fetch loads a whole cache line (~64 B) โŸน sequential access prefetches neighbours.
  • Multidim โž” row-major ; column-major of a row-major array thrashes the cache.

โš™๏ธ Core Implementation

๐Ÿ”น ArrayR โ€” fixed-size referential array

โš–๏ธ Core Decision Matrix

OperationComplexityReason
access / set array[i]address arithmetic
len(array)stored
create ArrayR(n)initialise slots
insert/delete in middleshift elements
grow beyond capacityreallocate + copy (Dynamic Array Resizing)

When It Flips: random access + cache-friendliness vs fixed capacity (growth = full copy) and 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:

Final Extracted Output: consecutive are adjacent in memory โŸน row-major (inner ) traversal is cache-optimal.

๐Ÿง  Active Recall