Sunday, May 12, 2019

C# : Jagged Array


Jagged Array : Jagged array is array of array.

Declaration of Jagged Array :

                                    int[][] jaggeedArry = new int[3][];

as mentioned before that Jagged array is array of array, in above declaration it's array of 3 arrays of int, and that 3 arrays are not necessary to be same in size. Let's understand by below example:

     public static void Main()
        {
            int[][] jaggeedArry = new int[3][];
           
            jaggeedArry[0] = new int[5]; // we can assign these arrays length at run time.
            jaggeedArry[1] = new int[3];
            jaggeedArry[2] = new int[2];

            Console.WriteLine(jaggeedArry.Length); //jagged array's length will be 3.

            //Inserting values in jaggedArray
            for (int i = 0; i < jaggeedArry.Length; i++){
                for(int j = 0; j < jaggeedArry[i].Length; j++)
                {
                    jaggeedArry[i][j] = 5 + i;
                }
            }
            //Printing values of jaggedArray
            for (int i = 0; i < jaggeedArry.Length; i++)
            {
                for (int j = 0; j < jaggeedArry[i].Length; j++)
                {
                    Console.Write(jaggeedArry[i][j] +"  ");
                }
                Console.WriteLine();
            }
            Console.Read();
        }

No comments:

Post a Comment

C# Record type: Something to remember while using record types

  Record in c# provide a concise and expressive way to create immutable data types, record is a keyword in c#, we can use this keyword with ...