joydip_kanjilal
Contributor

How to work with jagged arrays in C#

opinion
Sep 22, 20164 mins
Software Development

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

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.

joydip_kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author