C# | Jagged Arrays

Prerequisite: Arrays in C#

Jagged array is a array of arrays such that member arrays can be of different sizes. In other words, the length of each array index can differ. The elements of Jagged Array are reference types and initialized to null by default. Jagged Array can also be mixed with multidimensional arrays. Here, the number of rows will be fixed at the declaration time, but you can vary the number of columns.

Declaration

In Jagged arrays, user has to provide the number of rows only. If the user is also going to provide the number of columns, then this array will be no more Jagged Array.

Syntax:



data_type[][] name_of_array = new data_type[rows][]

Example:

int[][] jagged_arr = new int[4][]

In the above example, a single-dimensional array is declared that has 4 elements(rows), each of which is a 1-D array of integers.

Initialization

The elements of Jagged Array must be initialized before its use. You can separately initialize each array element. There are many ways to initialize the Jagged array’s element.

Example 1: Providing the size of each array elements separately. Here each of the elements is a 1-D array of integers where:

  • The first row or element is an array of 2 integers.
  • The second row or element is an array of 4 integers.
  • The third row or element is an array of 6 integers.
  • The fourth row or element is an array of 7 integers.
jagged_arr[0] = new int[2];
jagged_arr[1] = new int[4];
jagged_arr[2] = new int[6];
jagged_arr[3] = new int[7];

Example 2: When the array size is not needed then its elements can be initialized with direct values as follows:

jagged_arr[0] = new int[] {1, 2, 3, 4};
jagged_arr[1] = new int[] {11, 34, 67};
jagged_arr[2] = new int[] {89, 23};
jagged_arr[3] = new int[] {0, 45, 78, 53, 99};

Declaration as well as Initialization

Example 1: Using the Direct Method

int[][] jagged_arr = new int[][] 
{
    new int[] {1, 2, 3, 4},
    new int[] {11, 34, 67},
    new int[] {89, 23},
    new int[] {0, 45, 78, 53, 99}
};

Example 2: Using Short-hand Method. There is no default initialization for the elements so a user cannot omit the new operator from the elements initialization.

int[][] jagged_arr = 
{
    new int[] {1, 2, 3, 4},
    new int[] {11, 34, 67},
    new int[] {89, 23},
    new int[] {0, 45, 78, 53, 99}
};

Accessing the Elements

To access the elements of the Jagged array user has to specify the row and column with the array name.

Example:

// Accessing & Assigning 99 to the third element ([2]) of the second array ([1])
jagged_arr[1][2] = 99;

// Accessing & Assigning 47 to the first element ([0]) of the fourth array ([3]):
jagged_arr[3][0] = 47;

Program:


filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the declaration 
// and Initialization of Jagged Arrays
using System;
  
class GFG {
      
    // Main Method
    public static void Main()
    {
          
        // Declare the Jagged Array of four elements:
        int[][] jagged_arr = new int[4][];
  
        // Initialize the elements
        jagged_arr[0] = new int[] {1, 2, 3, 4};
        jagged_arr[1] = new int[] {11, 34, 67};
        jagged_arr[2] = new int[] {89, 23};
        jagged_arr[3] = new int[] {0, 45, 78, 53, 99};
  
        // Display the array elements:
        for (int n = 0; n < jagged_arr.Length; n++) {
  
            // Print the row number
            System.Console.Write("Row({0}): ", n);
  
            for (int k = 0; k < jagged_arr[n].Length; k++) {
  
                // Print the elements in the row
                System.Console.Write("{0} ", jagged_arr[n][k]);
            }
            System.Console.WriteLine();
        }
    }
}

chevron_right


Output:

Row(0): 1 2 3 4 
Row(1): 11 34 67 
Row(2): 89 23 
Row(3): 0 45 78 53 99 

Jagged Arrays With Multidimensional Arrays

It is possible to mix jagged and multidimensional arrays. Below are the declaration and initialization of a 1-D jagged array that contains four two-dimensional array elements of different sizes.

Example:

int[][, ] jagged_arr1 = new int[4][, ] 
{
    new int[, ] { {1, 3}, {5, 7} },
    new int[, ] { {0, 2}, {4, 6}, {8, 10} },
    new int[, ] { {7, 8}, {3, 1}, {0, 6} },
    new int[, ] { {11, 22}, {99, 88}, {0, 9} } 
};

A user can access the individual elements as shown in the below example, which displays the value of the element [2, 0] of the second array(i.e value is 8)

Example:

System.Console.Write("{0}", jagged_arr1[1][1, 0]);

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the Mixing of 1-D
// Jagged Array with the four 2-D array
using System;
namespace geeksforgeeks {
      
class GFG {
      
// Main Method
public static void Main()
{
  
    // Declaration and Initialization of 
    // Jagged array with 4 2-D arrays
    int[][, ] jagged_arr1 = new int[4][, ] {new int[, ] {{1, 3}, {5, 7}},
                                    new int[, ] {{0, 2}, {4, 6}, {8, 10}},
                                    new int[, ] {{7, 8}, {3, 1}, {0, 6}},
                                    new int[, ] {{11, 22}, {99, 88}, {0, 9}}};
  
    // Display the array elements:
    // Length method returns the number of
    // arrays contained in the jagged array
    for (int i = 0; i < jagged_arr1.Length; i++)
    {
          
        int x = 0;
          
        // GetLength method takes integer x which 
        // specifies the dimension of the array
        for (int j = 0; j < jagged_arr1[i].GetLength(x); j++) 
        {
              
            // Rank is used to determine the total 
            // dimensions of an array 
            for (int k = 0; k < jagged_arr1[j].Rank; k++)
                Console.Write("Jagged_Array[" + i + "][" + j + ", " + k + "]: "
                                            + jagged_arr1[i][j, k] + " ");
            Console.WriteLine();
        }
        x++;
        Console.WriteLine();
    }
}
}
}

chevron_right


Output:

Jagged_Array[0][0, 0]: 1 Jagged_Array[0][0, 1]: 3 
Jagged_Array[0][1, 0]: 5 Jagged_Array[0][1, 1]: 7 

Jagged_Array[1][0, 0]: 0 Jagged_Array[1][0, 1]: 2 
Jagged_Array[1][1, 0]: 4 Jagged_Array[1][1, 1]: 6 
Jagged_Array[1][2, 0]: 8 Jagged_Array[1][2, 1]: 10 

Jagged_Array[2][0, 0]: 7 Jagged_Array[2][0, 1]: 8 
Jagged_Array[2][1, 0]: 3 Jagged_Array[2][1, 1]: 1 
Jagged_Array[2][2, 0]: 0 Jagged_Array[2][2, 1]: 6 

Jagged_Array[3][0, 0]: 11 Jagged_Array[3][0, 1]: 22 
Jagged_Array[3][1, 0]: 99 Jagged_Array[3][1, 1]: 88 
Jagged_Array[3][2, 0]: 0 Jagged_Array[3][2, 1]: 9 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : shubham_singh



Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.