Jagged Array in C#
June 4, 2008Jagged array represents array of arrays where each array can have arbitrary number of elements. It’s the similar concept of double pointers in C.
Jagged array provides flexible services for manipulating and controlling data. In the case of C pointers will have to do the manual book keeping for better control over the bounds of data. We can have the same implementation by using dequeue/vector container classes in C++. But still we can’t say they’re representing array. Rather than they’re containers and provides common “container” interfaces.
You can check the number of facilities available for jagged arrays and arrays from MSDN. Also you will get some other good examples
How to use Jagged Arrays – Listing 1
// constructing jagged array
int[][] myJaggedArray = new int[5][];
for (int i = 0; i < myJaggedArray.Length; i++)
{
myJaggedArray[i] = new int[i + 1];
}
// iterating each arrays
for (int i = 0; i < myJaggedArray.Length; i++)
{
// iterating each elements in an array
for (int j = 0; j < myJaggedArray[i].Length; j++)
{
Console.Write( “{0} “, myJaggedArray[i][j]);
}
Console.WriteLine(” - {0} elements”, myJaggedArray[i].Length);
}
How to use Jagged Arrays – Listing 1
int[][,] jaggedArray4 = new int[3][,]
{
new int[,] { {1,3}, {5,7} },
new int[,] { {0,2}, {4,6}, {8,10} },
new int[,] { {11,22}, {99,88}, {0,9} }};
Posted by Sarath
