Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exploring a Possible Solution

Course: DSA › Arrays › Multidimensional Arrays

Now that we know the limitations of single-dimension arrays and the situations where those limitations prevent us from designing solutions, we can understand how we can extend the array to add more dimensions to it and design cleaner and more efficient solutions.


Multidimensional Arrays

A multidimensional array is an array of arrays. It is just a regular array, but instead of storing a primitive or user-defined datatype as its data item, it stores another array (single or multidimensional). The depth to which this nesting goes is called the dimension of the array.

There is no theoretical limit to how deep the nesting can go:

  • Single-dimension array — an array of non-array datatype
  • Two-dimension array — an array of arrays of non-array datatype
  • Three-dimension array — an array of arrays of arrays of non-array datatype
  • And this can go on…

Here’s what each looks like logically:

Single-dimension array — a flat row of values, accessed with one index:

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart LR
    v1["value1"] --- v2["value2"] --- v3["value3"] --- vn["· · ·"]
    size["◄────────── size ──────────►"] -.- v1

Single-dimension array — one row of elements, one index to access any element.

Two-dimensional array — a grid of rows and columns, accessed with two indices [row][col]:

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
block-beta
  columns 4
  r0c0["value1"] r0c1["value2"] r0c2["value3"] r0c3["· · ·"]
  r1c0["value4"] r1c1["value5"] r1c2["value6"] r1c3["· · ·"]
  r2c0["value7"] r2c1["value8"] r2c2["value9"] r2c3["· · ·"]
  r3c0["· · ·"]  r3c1["· · ·"]  r3c2["· · ·"]  r3c3["valueN"]

Two-dimensional array — a grid of size2 rows × size1 columns.

Three-dimensional array — a stack of 2D grids (layers), accessed with three indices [layer][row][col]:

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart TB
    subgraph L0["Layer 0  (a full size2 × size1 grid)"]
        direction LR
        l0r0["value1 │ value2 │ value3 │ · · ·"]
        l0r1["value4 │ value5 │ value6 │ · · ·"]
        l0r2["· · · │ · · · │ · · · │ valueN"]
    end

    subgraph L1["Layer 1  (a full size2 × size1 grid)"]
        direction LR
        l1r0["value1 │ value2 │ value3 │ · · ·"]
        l1r1["value4 │ value5 │ value6 │ · · ·"]
        l1r2["· · · │ · · · │ · · · │ valueN"]
    end

    subgraph LN["Layer N  (· · ·)"]
        direction LR
        lnr0["· · ·"]
    end

    L0 ~~~ L1 ~~~ LN

Three-dimensional array — size3 layers, each a full size2 × size1 two-dimensional grid.

Each additional dimension is just one more level of nesting — a 3D array is an array whose items are 2D arrays, just as a 2D array is an array whose items are 1D arrays.


Importance of Multidimensional Arrays

To understand how multidimensional arrays are useful and what a dimension represents, let us revisit the problem of storing the ages of all students in a class.


One-Dimensional Array

Instead of storing each student’s age in a separate variable, we can use a regular (one-dimensional) array to store all this data under a single variable. The size of the array is equal to the number of students in the class (size1).

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart LR
    label["age"] --> a1
    a1["value1"] --- a2["value2"] --- a3["value3"] --- a4["value4"] --- a5["value5"] --- a6["value6"] --- a7["value7"]
    span["◄── number of students in a class (size1) ──►"] -.- a1

Storing the age of students in a single class in a single-dimension array.

One array, one class. Simple and clean — as long as we only have one class.


Two-Dimensional Array

Let’s extend this problem to all classes (size2) in the school. We could store each class in a separate 1D array, but that won’t scale if we have hundreds of classes. A two-dimensional array solves this cleanly.

The idea: create an array of arrays where

  • the inner array stores the ages of all students in one class (size size1)
  • the outer array is a collection of those inner arrays, one per class (size size2)
---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart TB
    label["age"] --> c0

    c0["class 0 → value1 │ value2 │ value3 │ · · · │ valueN"]
    c1["class 1 → · · · │ · · · │ · · · │ · · · │ · · ·"]
    c2["class 2 → · · · │ · · · │ · · · │ · · · │ · · ·"]
    c3["class 3 → · · · │ · · · │ · · · │ · · · │ · · ·"]
    c4["class 4 → · · · │ · · · │ · · · │ · · · │ · · ·"]
    cn["class N → · · · │ · · · │ · · · │ valueX │ valueZ"]

    note_h["◄────── size1: number of students in a class ──────►"] -.- c0
    note_v["size2: classes"] -.- c0

Storing the age of students in all classes in a two-dimensional array.

The outer dimension (size2) represents how many classes there are. The inner dimension (size1) represents how many students are in each class. Together, size2 × size1 is the total number of values stored.


Three-Dimensional Array

Extending this further — what if we need to store the age of all students across all classes in all schools in a city (size3)?

Instead of creating multiple 2D arrays (one per school), we create a single three-dimensional array:

  • Size size3 (number of schools) at the outermost level
  • Each item is a 2D array of size size2 (classes)
  • Each item inside that is a 1D array of size size1 (students per class)
---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart TB
    label["age"] --> s0

    subgraph s0["School 0  (one 2D array of size size2 × size1)"]
        direction TB
        s0r0["class 0 → value1 │ value2 │ value3 │ · · · │ valueN"]
        s0r1["class 1 → · · · │ · · · │ · · · │ · · · │ · · ·"]
        s0rn["class N → · · · │ · · · │ · · · │ valueX │ valueZ"]
    end

    subgraph s1["School 1  (one 2D array of size size2 × size1)"]
        direction TB
        s1r0["class 0 → value5 │ value7 │ value8 │ · · · │ · · ·"]
        s1r1["class 1 → · · · │ · · · │ · · · │ · · · │ · · ·"]
        s1rn["class N → · · · │ · · · │ · · · │ · · · │ · · ·"]
    end

    sn["School N  (· · ·)"]

    note["size3: number of schools"] -.- s0

Storing the age of students across all classes in all schools in a three-dimensional array.

The three dimensions map directly onto the three levels of the real-world problem:

DimensionSizeRepresents
1st (innermost)size1students per class
2ndsize2classes per school
3rd (outermost)size3schools in the city

Access: age[school][class][student] — one index per dimension, from outermost to innermost.


Key Takeaway

A multidimensional array is nothing exotic — it’s just the natural next step. Every time you have a new level of grouping in your data, you add another dimension:

1 level of grouping → 1D array 2 levels of grouping → 2D array 3 levels of grouping → 3D array

Each new dimension is an outer array that holds the previous structure as its items. The total number of elements is always the product of all dimension sizes: size1 × size2 × ... × sizeN.