12.4 Mixing jagged and rectangular arrays


Developers who have mastered arrays, and want to put some complexity into their multi-dimensional arrays, can mix jagged and rectangular arrays. [7] Here is an example.

[7] I personally wouldn't do too much of this unless there really is good justification for doing so (such as sabotaging your colleague who is taking over your codes!).

 int[][,] MessyArray = new int [3][,] { new int[,] { {1,3}, {5,7} }, new int[,] { {0,2}, {4,6}, {8,10} }, new int[,] { {11,22,33}, {77,88,99} } }; 

This statement creates an int array called MessyArray , which is an array of two arrays. The first is an array of size 3 “ call them 3a , 3b , and 3c . 3a is a 2D array containing 2 rows of 2 cells each. 3b is a 2D array containing 3 rows of 2 cells each, and 2c is a 2D array of 2 rows of 3 cells each.

Figure 12.6 shows what MessyArray looks like.

Figure 12.6. MessyArray .

graphics/12fig06.gif

As an example, you can access the cell containing the value 22 via the expression:

 MessyArray[2][0,1] 

Despite the increased complexity of C# arrays, no matter how you declare them, arrays in C# can store only elements of the same type (as with Java arrays).



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net