Monday, May 21, 2007

Jagged Arrays

What are Jagged Arrays?
A Jagged Array is an array of an array in which the length of each array index can differ.
Example: A Jagged Array can be used to create a table in which the lengths of the row are not same. This array is declared using square brackets ([ ]) to indicate each dimension.
The following code demonstrates the creation of a two-dimensional jagged array.Class Jagged{public static void Main(){int [][] jagged=new int [3][];jagged[0]=mew int[4]jagged[1]=mew int[3]jagged[2]=mew int[5]int I;‘Storing values in first arrayfor (I=0;I<4;I++)jagged[0][I]=I;‘Storing values in second array

for( I=0;I<3;I++)
jagged[1][I]=I;
‘Storing values in third array

for(I=0;I<5;I++)
jagged[2][I]=I;
‘Displaying values from first array

for (I=0;I<4;I++)
Console.WriteLine(jagged[0][I])
‘Displaying values from second array

for (I=0;I<3;I++)
Console.WriteLine(jagged[1][I])

‘Displaying values from third array
for(I=0;I<5;I++)
Console.WriteLine(jagged[2][I])

}
}

No comments: