Arrays

The notes

What is an array?

A collection of elements of the same data type, stored in contiguous memory locations.

  • Ordered sequence
  • Fixed size

Structure and terms

  • Index: starts at 0
  • Element/value (example array: 5, 12, 8, 20 at indexes 0 to 3)
  • Size/length

Key properties

  • Consecutive memory: elements sit next to each other
  • Homogeneous (same type): all elements are the same type, such as integer or string
  • Fixed size: cannot easily grow or shrink

Common operations

  • Accessing: read an element by index, e.g. myArray[2]
  • Modifying: change an element, e.g. arr[1] = 99
  • Iterating (looping): visit the elements one by one, e.g. with a for-each loop

Analogies

  • Train carriages in a row
  • Numbered lockers
  • A row of post boxes

Data structures deck

  1. 1. Linear vs non-linear
  2. 2. Arrays
  3. 3. Hashmaps
  4. 4. Stacks
  5. 5. Queue
  6. 6. Linked list
  7. 7. Binary tree
  8. 8. BST
  9. 9. Graphs
  10. 10. Heaps

Watch the lesson

Data structures, Lesson 2, from 4:02: What is an array?