How To Use Jagged Arrays In C#

When working with multi-dimensional arrays, the speed can be improved by storing rows of data of variable lengths in jagged arrays.

 

 

 

 

 

 

 

 

 

 

 

A sequential collection of elements of the same data type may be referred to as an array. An array’s components are kept in adjacent memory regions. Arrays can have one dimension. A unique kind of multi-dimensional array called a jagged array allows each array to have a different size.

Any programming language that supports arrays allows for the creation of jagged arrays. A jagged array, also called a ragged array, is a collection of arrays, each of which can have a variety of shapes and sizes. To increase performance, multi-dimensional arrays and jagged arrays can be used.

Getting started with jagged arrays in C#

We will look at how to declare, initialize, and access jagged arrays in this section. As we all know, a jagged array is made up of several smaller or larger arrays. In a jagged array, the number of columns can vary but the number of rows is fixed. You can choose to specify the number of columns at runtime instead of declaring the number of rows when creating a jagged array.

Let’s use a few code samples to help us comprehend all we’ve learned so far about jagged arrays. Consider the array below.

string[][] str = new string[5][];

You have specified the array’s rows. This array has five rows, each of which has the potential to hold five string arrays of various lengths. Now let’s look at how we may define 5 different-sized arrays in the str array. This is demonstrated by the following piece of code.

str[0] = new string[5];
str[1] = new string[10];

str[2] = new string[20];

str[3] = new string[50];

str[4] = new string[10];

The code sample below demonstrates how to use the jagged array to hold strings

str[0][0] = "Pune";

str[1][0] = "Kolkata";

str[2][0] = "Bangalore";

str[3][0] = "The pink city named Jaipur";

str[4][0] = "Hyderabad";

The full code listing demonstrating how to declare a jagged array, store data, then retrieve it and display it in the console is provided below.

public static void Main(string[] args)

       {

         //First declare the jagged array

           string[][] str = new string[5][];

           str[0] = new string[5];

           str[1] = new string[10];

           str[2] = new string[20];

           str[3] = new string[50];

           str[4] = new string[10];

           //Now store data in the jagged array

           str[0][0] = "Pune";

           str[1][0] = "Kolkata";

           str[2][0] = "Bangalore";

           str[3][0] = "The pink city named Jaipur";

           str[4][0] = "Hyderabad";

            //Lastly, display the content of each of the string arrays inside the jagged array

           for (int i = 0; i < 5; i++)

               Console.WriteLine(str[i][0]);

           Console.Read();

       }

As you can see in the program above, the jagged array’s a fixed number of rows and a variable number of columns. This example shows a jagged, two-dimensional array. You would need to employ 5 x 50, or 250 bytes if you were to use a standard two-dimensional array. The biggest string would require 50 bytes of extra space in each of the jagged array arrays in order to fit it. The biggest string in this illustration is 50 inches long. On the other hand, you only use 95 bytes when utilizing a jagged array.

It’s fascinating, The strings kept in the jagged array are shown in the terminal window when the aforementioned software is run.

Another example of a jagged array of integers

You may make a jagged array of numbers in a similar way to how we made one for strings. In fact, every data type can have a jagged array. The declaration of a jagged array in C# is shown below.

int [][] numbersArray;

The following code snippet shows how to declare an integer jagged array or a jagged array that may store arrays of integers with different elements, in several ways.

int[][] numbersArray = new int[5][];

for (int i = 0; i < numbersArray.Length; i++)
{
    numbersArray[i] = new int[10 * (i + 1)];
}

The below code line generates an integer jagged array named numbersArray, which includes integer arrays of varied sizes.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories