Print Friendly and PDF
A Jagged array is an array of arrays. You can declare a jagged array namedscores of type int as: 1. There are 3 employees - kibriya, yahya, athar 2. kibriya has a bachelor, master and doctorate degree (3 qualifications) 3. yahya has only a bachelor degree (1 qualification) 4. athar has a bachelor and a master degree (2 qualifications)
A Jagged array is an array of arrays. You can declare a jagged array namedscores of type int as: 
 int [][] scores;
Declaring an array, does not create the array in memory. To create the above array:
int[][] scores = new int[5][];
for (int i = 0; i < scores.Length; i++)

scores[i] = new int[4];
 }

You can initialize a jagged array as: 

int[][] scores = new int[2][]{new int[]{92,93,94},new int[]{85,66,87,88}};
Where, scores is an array of two arrays of integers - scores[0] is an array of 3 integers and scores[1] is an array of 4 integers

Let us understand jagged array with an example

1. There are 3 employees - kibriya, yahya, athar
2. kibriya has a bachelor, master and doctorate degree (3 qualifications)
3. yahya has only a bachelor degree (1 qualification)
4. athar has a bachelor and a master degree (2 qualifications)

Solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace jaggedarrays
{
    public class Program
    {
        public static void Main()
        {
            // Create a string array to store the names of the employees
            string[] employeeNames = new string[3];
            employeeNames[0] = "kibriya";
            employeeNames[1] = "yahya";
            employeeNames[2] = "athar";
            // Create a jagged array. As we want to store varying number of qualifications
            // and as qualifications are strings, let's create an array of string arrays.
            string[][] jaggedArray = new string[3][];
            // Set the size of the first string array to 3, as we want to store 3 qualifications for kibriya
            jaggedArray[0] = new string[3];
            // Set the size of the second string array to 1, as we want to store
            // only 1 qualification for yahya
            jaggedArray[1] = new string[1];
            // Set the size of the third string array to 2, as we want to store 2 qualifications for athar
            jaggedArray[2] = new string[2];
            // Set the values for the first string array
            jaggedArray[0][0] = "Bachelor";
            jaggedArray[0][1] = "Master";
            jaggedArray[0][2] = "Doctorate";
            // Set the values for the second string array
            jaggedArray[1][0] = "Bachelor";
            // Set the values for the thrid string array
            jaggedArray[2][0] = "Bachelor";
            jaggedArray[2][1] = "Master"
            for (int i = 0; i < jaggedArray.Length; i++)
            {
                Console.WriteLine(employeeNames[i]);
                Console.WriteLine("--------");
                string[] innerArray = jaggedArray[i];
                for (int j = 0; j < innerArray.Length; j++)
                {
                    Console.WriteLine(innerArray[j]);
                }
                Console.WriteLine();
            }
         
            Console.ReadLine();
        }
    }
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: