Jagged Array
An array of arrays is called Jagged Array i.e. a jagged array is an array whose element is arrays. The element of jagged arraycan be of different size and dimension.Jagged Array Example
class JaggedArray{
static void Main()
{
// Declare jagged array of size 4.
int[][] objJaggedArray = new int[4][];
// Initialization jagged array.
objJaggedArray[0] = new int[4];
objJaggedArray[1] = new int[3];
objJaggedArray[2] = new int[5];
objJaggedArray[3] = new int[2];
objJaggedArray[0] = new int[] { 9, 8, 7, 6 };
objJaggedArray[1] = new int[] { 9, 8, 7 };
objJaggedArray[2] = new int[] { 9, 8, 7, 6, 5 };
objJaggedArray[3] = new int[] { 9, 8 };
Console.WriteLine("Jagged Array Items...");
// Display the jagged array elements:
for (int i = 0; i < objJaggedArray.Length; i++)
{
Console.WriteLine();
for (int j = 0; j < objJaggedArray[i].Length; j++)
{
Console.Write(objJaggedArray[i][j]+ " ");
}
}
Console.ReadLine();
}
}
Output
Jagged Array Items...9 8 7 6
9 8 7
9 8 7 6 5
9 8
No comments:
Post a Comment