Jagged Array in C#

Before reading this article, I highly recommend reading the previous part:

Question: What is a jagged array in C#?

Answer: A jagged array is an array of arrays.

Jagged array

  • The elements of a jagged array can be of varying dimensions and sizes.
  • In a jagged array the number of columns are determined depending on the requirements for each row.

A jagged array looks like this:

jagged array
Before proceeding let's understand this concept with an example.

We have the three employees Sourabh, Shaili and Shalin. Sourabh has three qualifications, Shaili has one qualification and Shalin has two qualifications. Here each employee has a different number of qualifications.

employee

Now, I want a data structure that stores this varying number of qualifications as a jagged array. Let's see an example of one of the choices for a jagged array.

Step 1: Open Visual Studio then create a new project and name it InterviewQuestionPart2.

create new project

console application

Step 2: Now the qualifications are for a string so I will create a string jagged array and initialize the three Arrays of string Array. For example:

  • The first employee Sourabh has 3 qualifications so the array of strings array has 3 elements.

  • The second employee Shaili has 1 qualification so the array of strings array has 1 element.

  • The third employee Shalin has 2 qualifications so the array of strings array has 2 elements.

  • Create an array of strings array depending on requirements.

  • Now we store the values of the first string array elements, like the first employee Sourabh has the three qualifications "Bachelors", "Masters" and "Doctorate" and we store the values.

  • Now initialize the second string array within the jagged array and store the values into the elements of the second string array like second employee Shaili has 1 qualification "Bachelors" so we store that value.

  • Now initialize the third string array within the jagged array and store the values to the elements of the third string array like the third employee Shalin has the 2 qualifications "Bachelors" and "Masters" so we store those values.

Create a jagged Array and String Array

Look at the following code:

  1. using System;    
  2. namespace InterviewQuestionPart2    
  3. {    
  4.     class Program    
  5.     {    
  6.         static void Main(string[] args)    
  7.         {      
  8.             //Creations of jaggedArray    
  9.             string[][] jaggedArray= new string[3][];    
  10.                
  11.             //Array of string Array    
  12.             jaggedArray[0] = new string[3];    
  13.             jaggedArray[1] = new string[1];    
  14.             jaggedArray[2] = new string[2];    
  15.                
  16.             //values of 1st string Array     
  17.     
  18.             jaggedArray[0][0] = "Bachelores";    
  19.             jaggedArray[0][1] = "Masters";    
  20.             jaggedArray[0][2] = "Doctorate";    
  21.     
  22.             //values of 2nd string Array    
  23.     
  24.             jaggedArray[1][0]= "Bachelores";    
  25.     
  26.             //values of 3rd string Array    
  27.     
  28.             jaggedArray[2][0] = "Bachelores";    
  29.             jaggedArray[2][1] = "Masters";    
  30.     
  31.         }    
  32.     }    
  33. }  
Step 3: Now we will use a loop and print the values of the jagged array, here we use the length property of Array that gives the count of of the array items within the jagged array, we have a three string array within the jagged array.

cs code

When we use a loop we get back each string array that are present in the jagged array and the values of the inner array means the values of the string array, with the following code.
  1. using System;    
  2. namespace InterviewQuestionPart2    
  3. {    
  4.     class Program    
  5.     {    
  6.         static void Main(string[] args)    
  7.         {      
  8.             //Creations of jaggedArray    
  9.             string[][] jaggedArray= new string[3][];    
  10.                
  11.             //Array of string Array    
  12.             jaggedArray[0] = new string[3];    
  13.             jaggedArray[1] = new string[1];    
  14.             jaggedArray[2] = new string[2];    
  15.                
  16.             //values of 1st string Array     
  17.     
  18.             jaggedArray[0][0] = "Bachelores";    
  19.             jaggedArray[0][1] = "Masters";    
  20.             jaggedArray[0][2] = "Doctorate";    
  21.     
  22.             //values of 2nd string Array    
  23.     
  24.             jaggedArray[1][0]= "Bachelores";    
  25.     
  26.             //values of 3rd string Array    
  27.     
  28.             jaggedArray[2][0] = "Bachelores";    
  29.             jaggedArray[2][1] = "Masters";    
  30.     
  31.            //for getting string array in jagged array    
  32.             for(int i=0;i<jaggedArray.Length;i++)    
  33.             {    
  34.                 string[] innerArray = jaggedArray[i];    
  35.                 //for getting values of innerArray or string Array    
  36.                 for(int j=0;j<innerArray.Length;j++)    
  37.                 {        
  38.                     //print the values    
  39.                     Console.WriteLine(innerArray[j]);    
  40.                 }    
  41.                 //for blank space    
  42.                 Console.WriteLine();    
  43.             }    
  44.     
  45.         }    
  46.     }    
  47. }  
Now see that in the output we have the employee Sourabh with 3 degrees ("Bachelors", "Masters", "Doctorate"), Shaili has 1 degree and Shalin has 2 degrees so it is printed like this:

output

Now in the output we want to see the names of the employee as well, so we need to create a String Array to store the employee names and the for loop to print the name of the employees for that string array.

Step 4: Now we will create a string array that contains the names of the employees and print the name of the employees, using the following complete code.
  1. using System;    
  2. namespace InterviewQuestionPart2    
  3. {    
  4.     class Program    
  5.     {    
  6.         static void Main(string[] args)    
  7.         {      
  8.             //String Array for Name of the Employee    
  9.     
  10.             string[] EmployeeNames = new string[3];    
  11.             EmployeeNames[0] = "Sourabh";    
  12.             EmployeeNames[1] = "Shaili";    
  13.             EmployeeNames[2] = "Shalin";    
  14.     
  15.             //Creations of jaggedArray for store qualification of employee    
  16.             string[][] jaggedArray= new string[3][];    
  17.                
  18.             //Array of string Array    
  19.             jaggedArray[0] = new string[3];    
  20.             jaggedArray[1] = new string[1];    
  21.             jaggedArray[2] = new string[2];    
  22.                
  23.             //values of 1st string Array     
  24.     
  25.             jaggedArray[0][0] = "Bachelores";    
  26.             jaggedArray[0][1] = "Masters";    
  27.             jaggedArray[0][2] = "Doctorate";    
  28.     
  29.             //values of 2nd string Array    
  30.     
  31.             jaggedArray[1][0]= "Bachelores";    
  32.     
  33.             //values of 3rd string Array    
  34.     
  35.             jaggedArray[2][0] = "Bachelores";    
  36.             jaggedArray[2][1] = "Masters";    
  37.     
  38.            //for getting string array in jagged array    
  39.            //print out the elements of jagged array    
  40.     
  41.             for(int i=0;i<jaggedArray.Length;i++)    
  42.             {    
  43.                 string[] innerArray = jaggedArray[i];    
  44.     
  45.                 //print values of string array EmployeeNames    
  46.                 Console.WriteLine(EmployeeNames[i]);    
  47.     
  48.                 //print the doted line under the each Employee name    
  49.                 Console.WriteLine("-------------");    
  50.     
  51.                 //for getting values of innerArray or string Array    
  52.                 for(int j=0;j<innerArray.Length;j++)    
  53.                 {        
  54.                     //print the values    
  55.                     Console.WriteLine(innerArray[j]);    
  56.                 }    
  57.                 //for blank space    
  58.                 Console.WriteLine();    
  59.             }    
  60.     
  61.         }    
  62.     }    
  63. }   
Now see the output with our expected results:

program output

So a jagged array is an array of arrays. In this example we have created an array of string arrays. In a similar way, we create an array of integer arrays, an array of decimal arrays, an array of Boolean arrays, and so on.