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,
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.
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.
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,
- using System;
- namespace InterviewQuestionPart2
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- string[][] jaggedArray= new string[3][];
-
-
- jaggedArray[0] = new string[3];
- jaggedArray[1] = new string[1];
- jaggedArray[2] = new string[2];
-
-
-
- jaggedArray[0][0] = "Bachelores";
- jaggedArray[0][1] = "Masters";
- jaggedArray[0][2] = "Doctorate";
-
-
-
- jaggedArray[1][0]= "Bachelores";
-
-
-
- jaggedArray[2][0] = "Bachelores";
- jaggedArray[2][1] = "Masters";
-
- }
- }
- }
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.
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.
- using System;
- namespace InterviewQuestionPart2
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- string[][] jaggedArray= new string[3][];
-
-
- jaggedArray[0] = new string[3];
- jaggedArray[1] = new string[1];
- jaggedArray[2] = new string[2];
-
-
-
- jaggedArray[0][0] = "Bachelores";
- jaggedArray[0][1] = "Masters";
- jaggedArray[0][2] = "Doctorate";
-
-
-
- jaggedArray[1][0]= "Bachelores";
-
-
-
- jaggedArray[2][0] = "Bachelores";
- jaggedArray[2][1] = "Masters";
-
-
- for(int i=0;i<jaggedArray.Length;i++)
- {
- string[] innerArray = jaggedArray[i];
-
- for(int j=0;j<innerArray.Length;j++)
- {
-
- Console.WriteLine(innerArray[j]);
- }
-
- Console.WriteLine();
- }
-
- }
- }
- }
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:
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.
- using System;
- namespace InterviewQuestionPart2
- {
- class Program
- {
- static void Main(string[] args)
- {
-
-
- string[] EmployeeNames = new string[3];
- EmployeeNames[0] = "Sourabh";
- EmployeeNames[1] = "Shaili";
- EmployeeNames[2] = "Shalin";
-
-
- string[][] jaggedArray= new string[3][];
-
-
- jaggedArray[0] = new string[3];
- jaggedArray[1] = new string[1];
- jaggedArray[2] = new string[2];
-
-
-
- jaggedArray[0][0] = "Bachelores";
- jaggedArray[0][1] = "Masters";
- jaggedArray[0][2] = "Doctorate";
-
-
-
- jaggedArray[1][0]= "Bachelores";
-
-
-
- jaggedArray[2][0] = "Bachelores";
- jaggedArray[2][1] = "Masters";
-
-
-
-
- for(int i=0;i<jaggedArray.Length;i++)
- {
- string[] innerArray = jaggedArray[i];
-
-
- Console.WriteLine(EmployeeNames[i]);
-
-
- Console.WriteLine("-------------");
-
-
- for(int j=0;j<innerArray.Length;j++)
- {
-
- Console.WriteLine(innerArray[j]);
- }
-
- Console.WriteLine();
- }
-
- }
- }
- }
Now see the output with our expected results:
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.