How to iterate a Multidimensional Array?
Last Updated :
07 Nov, 2022
Multidimensional arrays are arrays that have more than one dimension. For example, a simple array is a 1-D array, a matrix is a 2-D array, and a cube or cuboid is a 3-D array but how to visualize arrays with more than 3 dimensions, and how to iterate over elements of these arrays? It is simple, just think of any multidimensional array as a collection of arrays of lower dimensions.
n-D array = Collection of (n-1)D arrays
For example, a matrix or 2-D array is a collection of 1-D arrays.
2D Array is a collection of 1D Arrays
Similarly, you can visualize 3-D arrays and other multidimensional arrays.
How to iterate over elements of a Multidimensional array?
It can be observed that only a 1-D array contains elements and a multidimensional array contains smaller dimension arrays.
- Hence first iterate over the smaller dimension array and iterate over the 1-D array inside it.
- Doing this for the whole multidimensional array will iterate over all elements of the multidimensional array.
Example 1: Iterating over a 2-D array
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Considering 2-D array having 3 rows and 3 columns
int n = 3;
int m = 3;
int arr[][3]
= { { 3, 2, 7 }, { 2, 6, 8 }, { 5, 1, 9 } };
// Iterating over all 1-D arrays in 2-D array
for (int i = 0; i < n; i++) {
// Printing all elements in ith 1-D array
for (int j = 0; j < m; j++) {
// Printing jth element of ith row
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
Java
// Java Code for iterating over a 2D array.
import java.io.*;
class GFG {
public static void main(String[] args)
{
int n = 3;
int m = 3;
int[][] arr
= { { 3, 2, 7 }, { 2, 6, 8 }, { 5, 1, 9 } };
// Iterating over all 1-D arrays in 2-D array
for (int i = 0; i < n; i++) {
// Printing all elements of ith row
for (int j = 0; j < m; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
// This code is contributed by lokeshmvs21.
Python3
# python code to implement the approach
if __name__ == "__main__":
# Considering 2-D array having 3 rows and 3 columns
n = 3
m = 3
arr = [[3, 2, 7], [2, 6, 8], [5, 1, 9]]
# Iterating over all 1-D arrays in 2-D array
for i in range(0, n):
# Printing all elements in ith 1-D array
for j in range(0, m):
# Printing jth element of ith row
print(arr[i][j], end=" ")
print()
# This code is contributed by rakeshsahni
C#
using System;
public class GFG{
static public void Main (){
// Code
int n = 3;
int m = 3;
int [ , ] arr = { { 3, 2, 7 }, { 2, 6, 8 }, { 5, 1, 9 } };
// Iterating over all 1-D arrays in 2-D array
for (int i = 0; i < n; i++) {
// Printing all elements in ith 1-D array
for (int j = 0; j < m; j++) {
// Printing jth element of ith row
Console.Write(arr[i,j]);
Console.Write(" ");
}
Console.Write("\n");
}
}
}
// This code is contributed by akashish_.
JavaScript
<script>
// Considering 2-D array having 3 rows and 3 columns
let n = 3;
let m = 3;
let arr
= [[ 3, 2, 7 ], [ 2, 6, 8 ], [ 5, 1, 9 ] ];
// Iterating over all 1-D arrays in 2-D array
for (let i = 0; i < n; i++) {
// Printing all elements in ith 1-D array
for (let j = 0; j < m; j++) {
// Printing jth element of ith row
document.write(arr[i][j] + " ");
}
document.write("<br>");
}
// This code is contributed by satwik4409.
</script>
Time Complexity: O(n*m)
Auxiliary Space: O(1)
Example 2: Iterating over a 3D array
C++
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
// Consider 3-D array of dimensions 2*2*2
int x = 2, y = 2, z = 2;
int arr[][3][2] = { { { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } } };
// Iterating over each 2-D array in 3-D array
for (int i = 0; i < x; i++) {
cout << "Inside " << i + 1
<< " 2D array in 3-D array" << endl;
// Iterating over each 1-D array
for (int j = 0; j < y; j++) {
cout << "Inside " << j + 1
<< " 1D array of the 2-D array" << endl;
// Iterating over each element in 1-D array
for (int k = 0; k < z; k++) {
cout << arr[i][j][k] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}
Java
// Java Program for iterating over a 3D array.
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Consider 3-D array of dimensions 2*2*2
int x = 2, y = 2, z = 2;
int[][][] arr = { { { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } } };
// Iterating over each 2-D array in 3-D array
for (int i = 0; i < x; i++) {
System.out.println("Inside " + (i + 1)
+ " 2D array in 3D array");
// Iterating over each 1-D array
for (int j = 0; j < y; j++) {
System.out.println(
"Inside" + (j + 1)
+ " 1D array of the 2D array");
// Iterating over each element in 1-D array
for (int k = 0; k < z; k++) {
System.out.print(arr[i][j][k] + " ");
}
System.out.println();
}
System.out.println();
}
}
}
// This code is contributed by lokeshmvs21.
Python3
# Python code
# Consider 3-D array of dimensions 2*2*2
x = 2
y = 2
z = 2
arr = [ [ [ 1, 2 ], [ 3, 4 ] ],[ [ 5, 6 ], [ 7, 8 ] ] ]
# Iterating over each 2-D array in 3-D array
for i in range(0,x):
print("Inside " + str(i + 1) + " 2D array in 3-D array")
# Iterating over each 1-D array
for j in range(0,x):
print("Inside " + str(j + 1) + " 1D array of the 2-D array")
# Iterating over each element in 1-D array
for k in range(0,x):
print(arr[i][j][k],end=" ")
print("")
print("")
# This code is contributed by akashish__
C#
// C# Program for the iterating over a 3D array.
using System;
public class GFG {
static public void Main()
{
// Code
// Consider 3-D array of dimensions 2*2*2
int x = 2, y = 2, z = 2;
int[, , ] arr
= new int[2, 2, 2] { { { 1, 2 }, { 3, 4 } },
{ { 5, 6 }, { 7, 8 } } };
// Iterating over each 2-D array in 3-D array
for (int i = 0; i < x; i++) {
Console.Write("Inside " + (i + 1)
+ " 2D array in 3D array"
+ "\n");
// Iterating over each 1-D array
for (int j = 0; j < y; j++) {
Console.Write("Inside" + (j + 1)
+ " 1D array of the 2D array"
+ "\n");
// Iterating over each element in 1-D array
for (int k = 0; k < z; k++) {
Console.Write(arr[i, j, k] + " ");
}
Console.Write("\n");
}
Console.Write("\n");
}
}
}
// This code is contributed by lokeshmvs21.
JavaScript
<script>
// Driver code
// Consider 3-D array of dimensions 2*2*2
let x = 2;
let y = 2;
let z = 2;
let arr = [ [ [1, 2 ], [ 3, 4 ] ],
[ [ 5, 6 ], [ 7, 8 ] ] ];
// Iterating over each 2-D array in 3-D array
for (let i = 0; i < x; i++) {
document.write( "Inside " + (i + 1)
+ " 2D array in 3-D array" );
document.write("<br>");
// Iterating over each 1-D array
for (let j = 0; j < y; j++) {
document.write( "Inside " + (j + 1)
+" 1D array of the 2-D array" );
document.write("<br>");
// Iterating over each element in 1-D array
for (let k = 0; k < z; k++) {
document.write( arr[i][j][k] + " ");
}
document.write("<br>");
}
document.write("<br>");
}
// This code is contributed by satwik4409.
</script>
OutputInside 1 2D array in 3-D array
Inside 1 1D array of the 2-D array
1 2
Inside 2 1D array of the 2-D array
3 4
Inside 2 2D array in 3-D array
Inside 1 1D array of the 2-D array
5 6
Inside 2 1D array of the 2-D array
7 8
Time Complexity: O(x*y*z)
Auxiliary Space: O(1)
Similar Reads
Multidimensional Array in R
Arrays are the R data objects which can store data in more than two dimensions. For example: If we create an array of dimensions (2, 3, 4) then it creates 4 rectangular matrices each with 2 rows and 3 columns. These types of arrays are called Multidimensional Arrays. Arrays can store only data types
3 min read
C++ Multidimensional Array
A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.Syntax of Multidimensional ArraysC++data_type array_name[s1][s2]...[sn];where s1, s2,â¦, sn are the
8 min read
How to Get Value of Multidimensional Array in C?
Prerequisite: Array in C An array is a type of data structure where we can store multiple elements of similar data types. A multidimensional array can be termed an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. Declaration
8 min read
Initialization of Multidimensional Arrays in C++
In C++, multidimensional arrays are the type of arrays that have multiple dimensions, i.e., they can expand in multiple directions. In this article, we will discuss how to initialize the multidimensional arrays in C++. Methods to Initialize Multidimensional Array in C++We can initialize multidimensi
3 min read
Multidimensional Arrays in Scala
In Scala, multidimensional arrays can be represented as arrays of arrays, where each nested array represents a row or a column. The size of the array can be defined using two integer values representing the number of rows and columns, respectively. Here's an example code with output that demonstrate
4 min read
JavaScript Multidimensional Array
A multidimensional array in JavaScript is an array that contains other arrays as its elements. These are often used to represent data in a grid or matrix format. In JavaScript, there is no direct syntax for multidimensional arrays, but you can achieve this by creating arrays within arrays.Creating a
4 min read
Perl | Multidimensional Arrays
Multidimensional arrays in Perl are the arrays with more than one dimension. Technically there is no such thing as a multidimensional array in Perl but arrays are used to act as they have more than one dimension. Multi dimensional arrays are represented in the form of rows and columns, also knows as
6 min read
Multidimensional Arrays in Excel VBA
Multidimensional Arrays are used to store data of similar data types of more than one dimension. A multidimensional array has a dimension up to 60 but usually, we don't use arrays of dimensions more than 3 or 4. Here, we will see how to declare a multidimensional array in many ways and also how to c
2 min read
How does C allocate memory of data items in a multidimensional array?
The data items in a multidimensional array are stored in the form of rows and columns. Also, the memory allocated for the multidimensional array is contiguous. So the elements in multidimensional arrays can be stored in linear storage using two methods i.e., row-major order or column-major order. Ro
4 min read
Difference Between one-dimensional and two-dimensional array
Array is a data structure that is used to store variables that are of similar data types at contiguous locations. The main advantage of the array is random access and cache friendliness. There are mainly three types of the array: One Dimensional (1D) ArrayTwo Dimension (2D) ArrayMultidimensional Arr
3 min read