Skip to main content

What is a multidimensional array?

A multidimensional array is a data structure where elements are organized across several dimensions (rows, columns, levels, etc.), that is, an array made up of other arrays.

In simpler terms, it's a way to store data as a table, a matrix, or a cube, rather than a single long line.


1. Example of a two-dimensional array

A two-dimensional array can be pictured as a table:

javascript
A = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]

Here:

  • A[0][0] = 1
  • A[1][2] = 6
  • A[2][1] = 8

This is an array of 3 rows and 3 columns.


2. Three-dimensional array

Can be pictured as a "data cube":

javascript
B[x][y][z]

Example: B[2][1][0] is the element from the third layer, second row, first column.


3. Storage in memory

Even though visually this is a "table" or a "cube", in memory a multidimensional array is stored as a single contiguous sequence of elements.

There are two main placement methods:

  • Row-major order: by rows (C, C++, Python)
  • Column-major order: by columns (Fortran, MATLAB)

For example, for a 3×3 two-dimensional array, the elements [ [1,2,3], [4,5,6], [7,8,9] ] are stored one after another:

javascript
1, 2, 3, 4, 5, 6, 7, 8, 9

4. Formula for an element's address

For a two-dimensional array with n columns:

[ \text{address}(A[i][j]) = \text{base_address} + ((i \times n) + j) \times \text{size} ]

Where:

  • i is the row number,
  • j is the column number,
  • size is the size of one element.

5. Uses

Multidimensional arrays are used in:

  • mathematics (matrices, vectors),
  • graphics (image pixels),
  • machine learning (tensors),
  • games and simulation (three-dimensional spaces).

Summary:

A multidimensional array is an array whose elements are themselves arrays. It lets you store data as tables, matrices, or spatial grids and access elements using multiple indices.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.