| by Arround The Web | No comments

What are Jagged Arrays in C#

Jagged arrays are a type of multidimensional array in C# that allow you to create arrays of arrays. In a jagged array, each element of the array can hold another array of different sizes. Unlike a multidimensional array, a jagged array has rows of varying lengths, which gives it a “jagged” appearance.

This article will cover the details of jagged arrays in C#.

Following is the list of topics we will cover:

Table of Content:

1: How to Declare Jagged Arrays in C#

2: How to Initialize Jagged Arrays in C#

3: Initialization of Jagged Arrays Upon Declaration

Conclusion

1: How to Declare Jagged Arrays in C#

To declare a jagged array in C#, you need to specify the type of the array, followed by two sets of square brackets. The first set of square brackets specifies the number of rows in the array, and the second set of square brackets is left empty to indicate that the rows have varying lengths.

For example, to declare a jagged array of integers with 3 rows, you can write:

int[][] jaggedArray = new int[3][];

2: How to Initialize Jagged Arrays in C#

You can initialize a jagged array in C# by using the “new” keyword and specifying the size of each array row.

For example, to create a jagged array with 3 rows, where the first row has 2 elements, the second row has 3 elements, and the third row has 4 elements, you can write:

int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[2];

jaggedArray[1] = new int[3];

jaggedArray[2] = new int[4];

Here, we first create an array of 3 rows with new int [3] [ ]. Then, we create each row separately by specifying the size of each row with new int [2], new int [3], and new int [4] respectively.

2.1: Example Code to Initialize Jagged Arrays

This code demonstrates the use of a jagged array, which is an array of arrays where each sub-array can have a different length.

public class JaggedArrayTest

{  
    public static void Main()  
    {  
        int[][] jaggedArray = new int[3][];// Declare the array  

jaggedArray[0] = new int[] { 1};// Initialize the array          
jaggedArray[1] = new int[] { 2, 3};// Initialize the array          
jaggedArray[2] = new int[] { 4, 5, 6,};// Initialize the array  

        // Traverse array elements  
        for (int i = 0; i<jaggedArray.Length; i++)  
        {  
            for (int j = 0; j <jaggedArray[i].Length; j++)  
            {  
System.Console.Write(jaggedArray[i][j]+" ");  
            }  
System.Console.WriteLine();  
        }  
    }  
}

In the above example, the code declares and initializes a jagged array named “jaggedArray” of size 3, where the first sub-array has one element, the second sub-array has two elements, and the third sub-array has three elements.

The code then uses nested for loops to traverse the jagged array and print out its elements to the console. The outer loop iterates over each sub-array in jaggedArray, while the inner loop iterates over each element in the current sub-array.

Finally, the code prints each element of the jagged array to the console, separated by spaces, and prints a new line after each sub-array is printed.

Text Description automatically generated

3: Initialization of Jagged Arrays Upon Declaration

Initialization of a jagged array upon declaration in C# is done by enclosing the arrays within curly braces { } and separating the elements of each row with commas.

Here’s an example code syntax to declare and initialize a jagged array of integers with 3 rows, where the first row has 1 element, the second row has 2 elements, and the third row has 3 elements:

int[][] jaggedArray = new int[][] {

  new int[] { 1 },

  new int[] { 2, 3 },

  new int[] { 4, 5, 6 }

};

In the above code, we use the new keyword to create a new jagged array of integers, followed by [ ][ ] to specify that it’s a jagged array. Then, we enclose the rows within curly braces { }, and each row is represented by a new integer array with its respective elements.

Note that in this method of initialization, we don’t need to specify the size of each row explicitly because the compiler automatically infers it from the number of elements in each row.

3.1: Example Code to Initialize a Jagged Array Upon Deceleration

The below-given C# program demonstrates how one can declare and initialize a jagged array upon declaration, and then traverse its elements using nested for-loops.

public class JaggedArrayTest

{  
    public static void Main()  
    {  
    int[][] jaggedArray = new int[][] {
    new int[] { 1 },
    new int[] { 2, 3 },
    new int[] { 4, 5, 6 }
    };  
        // Traverse array elements  
        for (int i = 0; i<jaggedArray.Length; i++)  
        {  
            for (int j = 0; j <jaggedArray[i].Length; j++)  
            {  
System.Console.Write(jaggedArray[i][j]+" ");  
            }  
System.Console.WriteLine();  
        }  
    }  
}

In the above code we declared and initialized a jagged array of integers using curly braces { }.

Then, we use nested for loops to traverse the elements of the jagged array. The outer loop iterates over the rows of the array, and the inner loop iterates over the elements of each row.

Inside the inner loop, we use the System.Console.Write a method to print the current element of the array followed by a space, and then use System.Console.WriteLine to move to the next line after printing all the elements of the current row.

When we run this program, the output will be:

This shows that the program successfully declared and initialized a jagged array with 3 rows of varying lengths, and then printed its elements in a tabular format.

Conclusion

Jagged arrays in C# allow you to create arrays of arrays with varying row lengths. You can initialize a jagged array by specifying the size of each row using the “new” keyword or declare and initialize a jagged array in one line using curly braces. For more detail on jagged arrays in C, # read the guidelines above.

Share Button

Source: linuxhint.com

Leave a Reply