How to iterate over each element in a jagged array?

Asked by Cora Blake on
Tags: array element
7 Answers

Answer by Julius McIntyre

Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers , Stack Overflow help chat ,Thanks for contributing an answer to Stack Overflow!, Meta Stack Overflow

2D arrays are array of arrays... So one can iterate as:

   for (double[] row: multi) {
       for(double value: row) {
       }
   }

Source: https://stackoverflow.com/questions/40961513/how-to-iterate-over-each-element-in-a-jagged-array


Answer by Kenia McMahon

Assign Values to a Jagged Array,Iterating Through Jagged Array Using Nested Loops,C# provides another loop, named foreach, which you can use to iterate all elements in all rows of the jagged array. You can not use the foreach loop directly because a jagged array is an array of arrays, which means a nested foreach loop must access each row and columns within that row.,How to access values in a jagged array.

fruits[0][0] = "Apple";
fruits[0][1] = "Apricot";

fruits[1][0] = "Mango";
fruits[1][1] = "Orange";
fruits[1][2] = "Melon";

Source: https://codebuns.com/csharp-intermediate/jagged-array/


Answer by Norah Bender

You can’t use the foreach statement to directly iterate over all elements in a jagged array.  You can, however, iterate through the jagged array using nested foreach statements.  Each foreach statement independently iterates over one dimension of the jagged array., Filed under Statements Tagged with C#, foreach, Jagged Array, Statements , You are commenting using your Twitter account. ( Log Out /  Change ) , #1,191 - Lambda Can't Capture ref or out Parameters

You can’t use the foreach statement to directly iterate over all elements in a jagged array.  You can, however, iterate through the jagged array using nested foreach statements.  Each foreach statement independently iterates over one dimension of the jagged array.

            // Jagged array - 3 elements, each of which is array of int
            int[][] nums = new int[3][];

            nums[0] = new int[4];
            nums[2] = new int[3];

            for (int i = 0; i < 4; i++)
                nums[0][i] = i + 1;

            for (int i = 0; i < 3; i++)
                nums[2][i] = i + 101;

            // Iterate using foreach
            foreach (int[] intArray in nums)
            {
                if (intArray != null)
                {
                    Console.WriteLine("Array with {0} elements", intArray.Length);
                    foreach (int n in intArray)
                        Console.WriteLine("  {0}", n);
                }
                else
                    Console.WriteLine("Found null array");
            }

Source: https://csharp.2000things.com/2014/06/24/1124-iterate-through-jagged-array-with-nested-foreach/


Answer by Annalise Bishop

A jagged array is an array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These types of arrays are also known as Jagged arrays. ,Following is another example where i’th row has i columns, i.e., the first row has 1 element, the second row has two elements and so on.,Pictorial representation of Jagged array in Memory:,Alternative, ways to Initialize a Jagged array :


Syntax: data_type array_name[][] = new data_type[n][];  //n: no. of rows
             array_name[] = new data_type[n1] //n1= no. of colmuns in row-1
             array_name[] = new data_type[n2] //n2= no. of colmuns in row-2
             array_name[] = new data_type[n3] //n3= no. of colmuns in row-3
                                   .
                                   .
                                   .
             array_name[] = new data_type[nk]  //nk=no. of colmuns in row-n

Alternative, ways to Initialize a Jagged array :

                    int arr_name[][] = new int[][]  {
                                  new int[] {10, 20, 30 ,40},
                                  new int[] {50, 60, 70, 80, 90, 100},
                                  new int[] {110, 120}
                                      };
                                      
                              OR                                     
                                                         
                    int[][] arr_name = {
                          new int[] {10, 20, 30 ,40},
                          new int[] {50, 60, 70, 80, 90, 100},
                          new int[] {110, 120}
                              };
                              
                              OR                                     
                                                         
                    int[][] arr_name = {
                           {10, 20, 30 ,40},
                           {50, 60, 70, 80, 90, 100},
                           {110, 120}
                              };
Output
Contents of 2D Jagged Array
0 1 2 
3 4 
Output
Contents of 2D Jagged Array
0 
1 2 
3 4 5 
6 7 8 9 
10 11 12 13 14 

Source: https://www.geeksforgeeks.org/jagged-array-in-java/


Answer by Rebekah Avalos

Since 2D arrays are really arrays of arrays you can also use a nested enhanced for-each loop to loop through all elements in an array. We loop through each of the inner arrays and loop through all the values in each inner array. Notice the type of the outer loop array variable – it is an array that will hold each row!,We can loop through 2D arrays using nested for loops or nested enhanced for each loops.,You can loop through just part of a 2D array. You can change the starting value and ending value to loop through a subset of a 2D array.,In an enhanced for each loop, the variable of the outer loop must be the type of each row, which is a 1D array. The inner enhanced for loop variable must be the same type as the elements stored in the array.

In this case the for (int[] colArray : a) means to loop through each element of the outer array which will set colArray to the current column array. Then you can loop through the value in the column array.

for (int[] colArray : a)

Source: https://csawesome.runestone.academy/runestone/books/published/csawesome/Unit8-2DArray/topic-8-2-2D-array-loops-Day2.html


Answer by Miguel Church

Make the first element of each array its own size.,Or better yet, have an array of structs,Would I be able to iterate then?,If you don't require your code to work with jagged arrays of arbitrary structure, you could use a struct instead. This is less versatile, but since structs can have members of different types, they can represent any particular jagged array configuration with ease, as long as that configuration is known at compile time.

So following is my code:

#include <stdio.h>

int main(void) {
    int *const jagged[] = {
	    (int []) {0, 1},
	    (int []) {2, 3, 4}
    };
    return (0);
}

Source: https://www.reddit.com/r/C_Programming/comments/94ur4d/how_to_loop_through_a_jagged_array_in_c/


Answer by Brooks Calderon

You can also initialize a jagged array upon declaration like the below., A jagged array is an array of array. Jagged arrays store arrays instead of literal values. ,You can access a jagged array using two for loops, as shown below., The following example declares jagged arrays.

int[][] jArray1 = new int[2][]; // can include two single-dimensional arrays 
int[][,] jArray2 = new int[3][,]; // can include three two-dimensional arrays 

Source: https://www.tutorialsteacher.com/csharp/csharp-jagged-array