Showing posts with label Jagged Array. Show all posts
Showing posts with label Jagged Array. Show all posts

Jagged Array

What is Jagged Array?

Jagged array is called as "array of arrays". when you need a data inside your array element at that time you can use Jagged array.

How to declare Jagged Array?

You can declare Jagged array by following way, you can use any option by your choice.

1. int[][] jaggedArray=new int[3][]

2.int[][] jaggedArray=new int[3][]{
 
        new int[2]{1,2},
        new int[3]{1,2,3}
};

Let's see below example:

 static void Main(string[] args)
        {
            try
            {
                int[][] jaggedArray = new int[3][];

                //The element of JaggedArray must be defined. here we define size of 2.
                jaggedArray[0] = new int[2];
                jaggedArray[1] = new int[2];
                jaggedArray[2] = new int[2];

                //Set values in jaggedArray
                jaggedArray[0][0] = 1;
                jaggedArray[0][1] = 2;
                jaggedArray[1][0] = 3;
                jaggedArray[1][1] = 4;
                jaggedArray[2][0] = 5;
                jaggedArray[2][1] = 6;
                
                for(int i = 0; i < jaggedArray.Length; i++)
                {
                    for (int k = 0; k < 2; k++)
                    {
                        Console.WriteLine(jaggedArray[i][k]);
                    }
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

Output:
1
2
3
4
5
6

Jagged Array

What is Jagged Array? Jagged array is called as "array of arrays". when you need a data inside your array element at that time you...