Los arreglos multidimensionales son arreglos que tienen más de una dimensión. Por ejemplo, una array simple es una array 1-D, una array es una array 2-D y un cubo o paralelepípedo es una array 3-D, pero cómo visualizar arrays con más de 3 dimensiones y cómo iterar sobre elementos de estas arrays? Es simple, solo piense en cualquier array multidimensional como una colección de arrays de dimensiones más bajas .
array nD = Colección de arrays (n-1)D
Por ejemplo, una array o arreglo 2-D es una colección de arreglos 1-D.
De manera similar, puede visualizar arrays 3D y otras arrays multidimensionales.
¿Cómo iterar sobre elementos de una array multidimensional?
Se puede observar que solo una array 1-D contiene elementos y una array multidimensional contiene arrays de dimensiones más pequeñas.
- Por lo tanto, primero itere sobre la array de dimensiones más pequeñas e itere sobre la array 1-D dentro de ella.
- Hacer esto para toda la array multidimensional iterará sobre todos los elementos de la array multidimensional.
Ejemplo 1: Iterando sobre una array 2-D
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>
3 2 7 2 6 8 5 1 9
Ejemplo 2: Iterando sobre una array 3D
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>
Inside 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
Publicación traducida automáticamente
Artículo escrito por ishankhandelwals y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA