Multi-Dimensional Arrays
1. Definition
A multi-dimensional array is an array of arrays, where each element of the main
array can itself be an array. It allows you to store data in a table-like structure (2D
array) or even in a higher-dimensional structure (3D, 4D, etc.).
For example:
A 2D array represents a matrix, where each element is accessed using two
indices: one for the row and one for the column.
A 3D array represents a cube of data, where you need three indices to
access an element.
2. Two-Dimensional Arrays (2D Arrays)
A 2D array is an array of arrays, commonly used to represent a matrix (a table
with rows and columns). It can be thought of as a grid where each element is
accessible via two indices: one for the row and one for the column.
Declaration and Initialization (Example in Different Languages)
C/C++: In C and C++, a 2D array is declared with two indices.
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Java: In Java, 2D arrays are objects and are accessed in a similar way.
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
, {7, 8, 9}
};
Python: Python doesn't have a built-in 2D array, but you can use lists to
create a 2D structure.
arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
JavaScript: JavaScript arrays can hold arrays as elements, so a 2D array is
created similarly.
let arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Accessing Elements
To access elements in a 2D array, you use two indices (row and column).
C/C++:
int value = arr[1][2]; // Access the element at row 1, column 2 (value 6)
Python:
value = arr[1][2] # Access the element at row 1, column 2 (value 6)
JavaScript:
let value = arr[1][2]; // Access the element at row 1, column 2 (value 6)
1. Definition
A multi-dimensional array is an array of arrays, where each element of the main
array can itself be an array. It allows you to store data in a table-like structure (2D
array) or even in a higher-dimensional structure (3D, 4D, etc.).
For example:
A 2D array represents a matrix, where each element is accessed using two
indices: one for the row and one for the column.
A 3D array represents a cube of data, where you need three indices to
access an element.
2. Two-Dimensional Arrays (2D Arrays)
A 2D array is an array of arrays, commonly used to represent a matrix (a table
with rows and columns). It can be thought of as a grid where each element is
accessible via two indices: one for the row and one for the column.
Declaration and Initialization (Example in Different Languages)
C/C++: In C and C++, a 2D array is declared with two indices.
int arr[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Java: In Java, 2D arrays are objects and are accessed in a similar way.
int[][] arr = {
{1, 2, 3},
{4, 5, 6},
, {7, 8, 9}
};
Python: Python doesn't have a built-in 2D array, but you can use lists to
create a 2D structure.
arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
JavaScript: JavaScript arrays can hold arrays as elements, so a 2D array is
created similarly.
let arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
Accessing Elements
To access elements in a 2D array, you use two indices (row and column).
C/C++:
int value = arr[1][2]; // Access the element at row 1, column 2 (value 6)
Python:
value = arr[1][2] # Access the element at row 1, column 2 (value 6)
JavaScript:
let value = arr[1][2]; // Access the element at row 1, column 2 (value 6)