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

Overview of Supported Operations

Course: DSA › Arrays › Multidimensional

Now that we know the logical representation of a multidimensional array, let’s look at how to create, access, modify, and traverse one. In most modern programming languages, these operations are very similar and follow the same rules as those of a single-dimensional array.


Construction

Almost all major programming languages support adding more dimensions to a regular array in one form or another. Since a multidimensional array is just an array, it has a fixed size that cannot be modified after creation. All data items in the array must be of the same data type.

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
block-beta
  columns 3
  A["value1"] B["value2"] C["value3"]
  D["value4"] E["value5"] F["value6"]
  G["value7"] H["value8"] I["value9"]

Creating a multidimensional array of fixed size and datatype.

Higher-level languages like Python and JavaScript inherently provide a list instead of a raw array. A list has a dynamic size and can store elements of different types — so the programmer doesn’t need to provide a size when declaring or initializing a multidimensional array.

from typing import List

# Declaring and initializing a 2D array (list of lists)
numbers2d: List[List[int]] = [
    [1, 2, 3],
    [4, 5, 6]
]

# Declaring and initializing a 3D array (list of list of lists)
numbers3d: List[List[List[int]]] = [
    [ [1, 2], [3, 4], [5, 6] ],
    [ [7, 8], [9, 10], [11, 12] ]
]

print("2D array:", numbers2d)
print("3D array:", numbers3d)

Tip: In Python, there’s no built-in multidimensional array type — you nest lists inside lists. The type annotation List[List[int]] is just a hint, but it clearly communicates the intended shape.


Accessing Elements

We can access data items in a multidimensional array just like a regular array — using the subscript operator [] and an index. Since every data item in a multidimensional array is itself an array, we chain the subscript operator to drill into each dimension. We keep chaining until we reach a non-array data item.

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
block-beta
  columns 3
  A["[0,0]<br/>value1"] B["[0,1]<br/>value2"] C["[0,2]<br/>value3"]
  D["[1,0]<br/>value4"] E["[1,1]<br/>value5"] F["[1,2]<br/>value6"]
  G["[2,0]<br/>value7"] H["[2,1]<br/>value8"] I["[2,2]<br/>value9"]

Multidimensional array elements can be accessed using indices for all dimensions.

The pattern generalises naturally:

  • 2D: array[row][col]
  • 3D: array[depth][row][col]
  • N-D: keep chaining [index] for each additional dimension

Different programming languages have different syntax, but the underlying access mechanism is the same.

from typing import List

# Initializing a 2D array
numbers2d: List[List[int]] = [
    [1, 2, 3],
    [4, 5, 6]
]

# Accessing elements using [row][column]
print("Element at (0,0):", numbers2d[0][0])  # → 1
print("Element at (1,2):", numbers2d[1][2])  # → 6

# Initializing a 3D array
numbers3d: List[List[List[int]]] = [
    [ [1, 2], [3, 4], [5, 6] ],
    [ [7, 8], [9, 10], [11, 12] ]
]

# Accessing elements using [depth][row][column]
print("Element at (0,1,1):", numbers3d[0][1][1])  # → 4
print("Element at (1,2,0):", numbers3d[1][2][0])  # → 11

Think of it as unpacking layers. numbers2d[1] gives you the entire second row (an array). numbers2d[1][2] then picks the third element from that row. Each [] unwraps one layer.


Modifying Elements

We can modify data items in a multidimensional array in place, just like a regular array. Chain the subscript operator as many times as there are dimensions to reach the target element, then assign the new value on the right-hand side.

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
block-beta
  columns 3
  A["[0,0]<br/>value1"] B["[0,1]<br/>value2"] C["[0,2]<br/>value3"]
  D["[1,0]<br/>value4"] E["[1,1]<br/>value5"] F["[1,2]<br/>value6"]
  G["[2,0]<br/>value7"] H["[2,1]<br/>value8"] I["[2,2]<br/>value9"]

  style E fill:#fde68a,stroke:#d97706,color:#1a1a1a
  style I fill:#fde68a,stroke:#d97706,color:#1a1a1a

Multidimensional array elements can be modified using indices for all dimensions (highlighted = being updated).

from typing import List

# Initializing a 2D array
numbers2d: List[List[int]] = [
    [1, 2, 3],
    [4, 5, 6]
]

# Modifying elements in the 2D array
numbers2d[1][1] = 60

print("Modified 2D array:", numbers2d)  # → [[1, 2, 3], [4, 60, 6]]

# Initializing a 3D array
numbers3d: List[List[List[int]]] = [
    [ [1, 2], [3, 4], [5, 6] ],
    [ [7, 8], [9, 10], [11, 12] ]
]

# Modifying elements in the 3D array
numbers3d[0][1][1] = 40
numbers3d[1][1][1] = 110

print("Modified 3D array:", numbers3d)

Different languages implement the syntax differently, but the result is the same — overwrite the value at the memory location identified by chaining the indices.


Traversal

To traverse a multidimensional array, we need nested loops — one loop for each dimension. The logic is a direct extension of single-dimensional traversal: each loop iterates over the indices of one dimension.

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart LR
    S(["Start"]) --> OL["Outer loop<br/>i = 0..rows"]
    OL --> IL["Inner loop<br/>j = 0..cols"]
    IL --> V["Visit array[i][j]"]
    V --> IL
    IL -->|"inner done"| OL
    OL -->|"outer done"| E(["End"])

Traversing a 2D array requires two nested loops — one per dimension.

from typing import List

# Initializing a 2D array
numbers2d: List[List[int]] = [
    [1, 2, 3],
    [4, 5, 6]
]

# 1. Index-based for loop (2D)
print("2D array traversal (index-based):")
for i in range(len(numbers2d)):
    for j in range(len(numbers2d[i])):
        print(numbers2d[i][j], end=" ")
    print()

# 2. For-each loop (2D)
print("2D array traversal (for-each):")
for row in numbers2d:
    for value in row:
        print(value, end=" ")
    print()

For a 3D array, just add one more nesting level:

from typing import List

# Initializing a 3D array
numbers3d: List[List[List[int]]] = [
    [ [1, 2], [3, 4], [5, 6] ],
    [ [7, 8], [9, 10], [11, 12] ]
]

# 3D traversal
print("3D array traversal:")
for matrix in numbers3d:
    for row in matrix:
        for value in row:
            print(value, end=" ")
        print(" ", end="")
    print()

Important: The order of these loops affects performance depending on the order in which array items are stored in memory. We’ll explore this in detail when we cover how a multidimensional array is laid out in memory (row-major vs column-major order).


Summary

OperationSyntax (2D)Time Complexity
Createarr = [[0]*cols for _ in range(rows)]O(n)
Accessarr[row][col]O(1)
Modifyarr[row][col] = xO(1)
Traversenested for loopsO(n)

Access and modify are O(1) — the CPU computes the exact memory address from the indices directly, no searching required. Traversal is O(n) because every element must be visited.