The array elements might be arrays themselves, in which case the array is called a jagged array or an array of arrays.
Arrays descend from the System.Array class, which serves as the base class for all arrays in the Common Language Runtime.
It is possible to declare an array of any type (reference or value type), as in this example, which declars an array of strings:
string[] myStringArray = new string[10];
The difference between value-type elements and reference – type elements is that value- type elements are initialized to their default values, while those of reference type are initialized with the value null.
To declare a jagged array with the name myJaggedArray, use a statement like this:
int[][] myJaggedArray = ne int[2][] {new int[] {2,3,4}, new int[] {5,6,7,8,9} };
This array is a one – dimensional array that contains two elements. Each element is a one – dimensional array with a different number of elements; in this case, three and five.
It is possible to omit the dimensions of the first array, like this:
int[][] myJaggedArray = new int[][] { new int[]{2,3,4}, new int[] {5,6,7,8,9}};
we can also omit the first new operator:
int[][] myJaggedArray = { new int[] {2,3,4}, new int[]{5,6,7,8,9}};
The following statements access the elements of myJagged Array and assign the value 11 to the first element of the first array and the value 22 to the third element of the second array:
myJaggedArray[0][0] = 11;
myJaggedArray[1][2] = 22;
Tuesday, March 16, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment