Jagged Array is arrays of array in .NET Framework. Below is the example to create Jagged Array in C#.
int [][] jaggedArray = new int [5][];
jaggedArray[0] = new int [5];
jaggedArray[1] = new int [6];
jaggedArray[2] = new int [7];
jaggedArray[3] = new int [8];
jaggedArray[4] = new int [9];
Above declaration creates 2 Dimensional array with different lengths for each row. These index shows the size arrays are and referenced as Jagged Arrays.
The below code shows example of Jagged array in C#.
using
System;public class JaggedArrayExample{
public static void Main() {
// Declaration of Jagged Arrayint [][] jaggedArray = new int [5][]; jaggedArray[0] =
new int [5]; jaggedArray[1] =
new int [6]; jaggedArray[2] =
new int [7]; jaggedArray[3] =
new int [8];jaggedArray[4] =
new int [9]; Console.WriteLine("Enter numbers for Jagged Array to display"); for(int i=0 ; i < jaggedArray.Length ; i++) {
for(int j=0 ; j < jaggedArray[i].Length ; j++) {
String arrStr= Console.ReadLine(); int n=Int32.Parse(arrStr); jaggedArray[i][j]= n;
}
}
Console.WriteLine("Result of Jagged Array Elemnts are like below for the entered number"); for(int i=0 ; i < jaggedArray.Length ; i++) {
for(int j=0 ; j < jaggedArray[i].Length ; j++) {
Console.Write(jaggedArray[i][j]+" "); }
Console.WriteLine("\t"); }
}
}
Hope this help to beginner to understand Jagged Array concept in C#.