C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.

C# Jagged Arrays Declaration and initialization Example :
using System;
public class ArrayExample
{
            public static void Main (string[] args)
            {
                        int[][] arr=new int[2][];
                        arr[0]=new int[3]{10,12,13};
                        arr[1]=new int[3]{11,13,14};
            }
}
C# jagged Array Example :  Traversing the Jagged array
using System;
public class ArrayExample
{
            public static void Main (string[] args)
            {
                        int[][] arr=new int[2][];
                        arr[0]=new int[3]{10,12,13};
                        arr[1]=new int[3]{11,13,14};
                        for(int i=0;i<arr.Length;i++)
                        {
                                    for(int j=0;j<arr[i].Length;j++)
                                    {
                                                Console.Write(arr[i][j]+ " ");
                                    }
                                    Console.WriteLine();
                        }
            }
}
Output
10 12 13
11 13 14



ADVERTISEMENT
ADVERTISEMENT