A multidimensional array is an array of arrays. Instead of a simple linear structure (like a 1D array), you have multiple dimensions — each index leads to another array.
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
This can be visualized as a table with 3 rows and 4 columns.
| Type | Description | Example Dimensions |
|---|---|---|
| 1D Array | Linear collection of elements | [1, 2, 3, 4] |
| 2D Array | Array of arrays | [[1,2],[3,4]] |
| 3D Array | Array of 2D arrays | [[[1],[2]],[[3],[4]]] |
| N-D Array | Extends to N dimensions (rare) | N > 3 |
int arr[3][4];
int arr[2][2][3];
int[][] arr = new int[3][4];
int[][][] arr = new int[2][2][3];
arr = [[1, 2], [3, 4]]
import numpy as np
arr = np.array([[1, 2], [3, 4]])
let arr = [
[1, 2],
[3, 4]
];
arr[1][2]
arr[1][2][0]
arr[2][3] = {
{1,2,3},
{4,5,6}
}
Memory: 1 2 3 4 5 6
Memory: 1 4 2 5 3 6
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
cout << arr[i][j];
for row in arr:
for col in row:
print(col)
| Use Case | Description |
|---|---|
| Matrix Operations | Used in math-heavy computations |
| Images (Pixels) | Each pixel has rows and columns |
| Gaming (Grids, Boards) | For representing chess, maze, etc. |
| Tabular Data | Store spreadsheets, table records |
| Simulations | Weather, physics, etc. |
| Deep Learning Tensors | Used in neural networks |
| Operation | Example Syntax (Python) | Meaning |
|---|---|---|
| Access | arr[i][j] | Get value |
| Update | arr[i][j] = 10 | Set value |
| Append Row | arr.append([7,8]) | Add row |
| Remove Row | arr.pop(1) | Remove row |
| Length | len(arr) | No. of rows |
| Column Length | len(arr[0]) | No. of columns |
let board = [
['X', 'O', 'X'],
['O', 'X', ''],
['X', '', 'O']
];
image = [
[0, 255, 128],
[128, 128, 255],
[0, 64, 128]
]
| Mistake | Why It Happens | Fix |
|---|---|---|
| Index Out of Bounds | Accessing beyond limit | Validate indices |
| Jagged Array Confusion | Assuming all rows same length | Check row lengths |
| Memory Misunderstanding | Especially in C/C++ | Understand memory layout |
| Wrong Loop Order | Reversed loop indexing | Ensure correct order |
| Language | Multi-D Support | Jagged Arrays | Row-major |
|---|---|---|---|
| C/C++ | Yes | No | Yes |
| Java | Yes (Jagged default) | Yes | Yes |
| Python | No (via lists) | Yes | Yes |
| JavaScript | No (via arrays) | Yes | Yes |
| MATLAB | Yes | No | No |
Matrix:
[ [1, 2, 3],
[4, 5, 6] ]
Memory:
Index -> 0 1 2 3 4 5
Element-> 1 2 3 4 5 6
arr[2][2][2] = [
[ [1, 2], [3, 4] ],
[ [5, 6], [7, 8] ]
]
Code:
let arr = Array.from({ length: 3 }, () =>
Array.from({ length: 4 }, () => Math.floor(Math.random() * 100))
);
process.stdout.write(`${JSON.stringify(arr)}\\n`);
Explanation:
Array.from({ length: 3 }) → Creates 3 rowsArray.from({ length: 4 }) → Each row has 4 columnsMath.random() * 100 → Random number 0–99JSON.stringify(arr) → Converts array to stringprocess.stdout.write → Prints output to terminalSample Output:
[[14, 73, 6, 91],
[65, 43, 9, 87],
[27, 88, 19, 54]]