How to work with jagged arrays in C#

Jagged arrays are a special type of arrays that can be used to store rows of data of varying lengths to improve performance when working with multi-dimensional arrays

Jagged Array

Jagged Array

An array may be defined as a sequential collection of elements of the same data type. The elements of an array are stored in contiguous memory locations. Arrays can be single or multi-dimensional. A jagged array is a special type of a multi-dimensional array in which each of the array (a jagged array is actually an array of arrays) can be of varying size.

You can have jagged arrays in any computer language that provides support for arrays. A jagged array (also known as a ragged array) is an array of arrays in which the member arrays in turn can be of different dimensions and sizes. You may implement multi-dimensional arrays are jagged arrays to improve performance.

Getting started with jagged arrays in C#

In this section we will explore how to declare, initialize and access jagged arrays. As we know, a jagged array comprises of an array of arrays of similar or different sizes. In other words, in a jagged array, the number of rows is fixed, but the number of columns may vary. When declaring a jagged array, you can just declare the number of rows of the array and prefer to specify the number of columns at runtime.

Let us know understand all that we have learnt so far on jagged arrays with a few code examples. Consider the following array.

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

You have declared the rows of the array. There are 5 rows in this array that can in turn contain 5 string arrays of different lengths. Let’s now see how we can declare 5 arrays in the array named str, each of different lengths. The following code snippet illustrates how this can be achieved.

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];

You can now store strings of dissimilar lengths in the jagged array as shown in the code snippet below.

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

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

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

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

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

Here’s the complete code listing that illustrates how you can declare a jagged array, store data and then retrieve and display it in the console.

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 above program, the number of rows of the jagged array is fixed but the number of columns vary. This example represents a two – dimensional jagged array. If you were to use a normal two – dimensional array, you would have to consume 5 x 50, i.e., 250 bytes. The reason is that you would have to have space of 50 bytes in each of the arrays in the jagged array to accommodate the largest string. In this example, the largest string is of size 50. On the contrary, in using a jagged array, you end up consuming just 95 bytes! Interesting, isn’t it?

When you execute the above program, the strings stored in the jagged array are displayed in the console window.

Another example -- a jagged array of integers

Similar to how we have created jagged array of strings, you can also create jagged array of integers. In fact, you can have a jagged array of any data type. Here's how you can declare a jagged array in C#.

int [][] numbersArray;

The following code snippet illustrates how you can declare an integer jagged array, i.e., a jagged array that can in turn store arrays of integers of varying elements.

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

for (int i = 0; i < numbersArray.Length; i++)

   {

         numbersArray[i] = new int[10 * (i + 1)];

   }

The above code snippet creates an integer jagged array named numbersArray which in turn contains integer arrays of varying sizes.

This article is published as part of the IDG Contributor Network. Want to Join?

From CIO: 8 Free Online Courses to Grow Your Tech Skills
Notice to our Readers
We're now using social media to take your comments and feedback. Learn more about this here.