Arrays of More Than Two Dimensions

   


As mentioned earlier, C# lets you work with arrays of more than two dimensions. For example, if you want to collect elevator requests from 10 elevators instead of just one, and for 7 days/24 hours, you can match this requirement with a three-dimensional rectangular array declared as shown in Figure 11.12. The number of commas in the square brackets of the declaration must now be two instead of one; and three numbers, each specifying the length of a dimension, are needed when creating the three-dimensional array object.

Figure 11.12. Declaring and defining the three-dimensional rectangular requests array.
graphics/11fig12.gif

To traverse an entire three-dimensional array calls for three nested loops (see Figure 11.13) with the indexed array element positioned in the loop body of the innermost loop.

Figure 11.13. Three nested for loops traversing three-dimensional array requests incomplete code.
graphics/11fig13.gif

Syntax Box 11.1 Declaring and Creating Rectangular Multidimensional Arrays

 Multidimensional_rectangular_array_variable_declaration::= <Base_type> [ , [ ,..., ] ] <Array_identifier>; 

Note:

  • The number of commas in the square brackets <Base_type> is one less than the number of dimensions.

 Multidimensional_rectangular_array_object_creation::= new <Base_type> [<Length_0>, <Length_1> [ , <Length_2> ..., <Length_n>] graphics/ccc.gif ] 

Note:

  • <Length_0> is the length of the first dimension.

 Assigning reference of new multidimensional rectangular array  graphics/ccc.gifobject to array variable::= <Array_identifier> = new <Base_type>[<Length_0>, <Length_1> [ ,  graphics/ccc.gif<Length_2> ..., <Length_n> ] ]; 

Arrays of three or more dimensions can be jagged. To understand how they are specified, we can imagine them as arrays of arrays of arrays of…and so on. For example, a three-dimensional array can be viewed as an array of two-dimensional arrays, and each two-dimensional array can again be viewed as an array of arrays. Consequently, the previously mentioned rectangular array could be regarded as an array with ten array elements each holding a two-dimensional array. Each of these two-dimensional arrays could again be viewed as an array with 7 array elements, each representing an array of 24 entries. Specifying requests to be a jagged array with ten two-dimensional arrays can be done with the following line:

 ushort [] [] [] requests = new [10] [] []; 

If we wanted the first dimension of the first two-dimensional array to have seven entries, we could write the following line:

 requests [0] = new ushort [7][]; 

In our elevator statistics analogy, it means that the first elevator will have space for seven days worth of requests numbers.

Finally, we could specify the third day of the first elevator to have, say, 18 entries, and thereby allow us to record 18 hours:

 requests [0] [2] = new ushort [18]; 

As you can imagine, it is an elaborate job to explicitly write out all the sizes of the different parts of a multidimensional jagged array. However, if the various sizes are determined by a logic system, it is possible to automate the array object creation process with suitable loops.

Syntax Box 11.2 Declaring and Creating Jagged Multidimensional Arrays

 Multidimensional_jagged_array_variable_declaration::= <Base_type> [ ] [ ]  [ [ ]...[ ] ] <Array_identifier>; 

Note:

  • The number of square bracket pairs is equal to the number of dimensions of the array.

  • As usual, [] denotes the square brackets that you see in the source code, whereas [] encloses optional source code parts.

 Multidimensional_jagged_array_object_creation::= new <Base_type> [<Length_0>] [<Length_1>]  [  [<Length_2>] [<Length_3>]...[<Length_n>]  ] 

Note:

  • <Length_0> is the length of the first dimension.

 Assigning reference of new multidimensional jagged array object  graphics/ccc.gifto array variable: <Array_identifier> = new <Base_type> [<Length_0>] [<Length_1>]  [ [<Length_2>] [<Length_3>]...[<Length_n>] ] 

Accessing Multidimensional Arrays with the foreach Statement

Earlier, you saw how a single foreach statement could be used to traverse an entire one-dimensional array. A foreach statement can also be applied to access multidimensional arrays. When the foreach statement is used to traverse a rectangular array, the syntax is extremely simple compared to the nested for loops. For example, to traverse the entire three-dimensional requests array declared and defined as follows

 ushort [ , , ] requests = new ushort [10,7,24]; 

so the program can calculate the sum of the values of all its array elements, we merely need to write one foreach statement:

 foreach (ushort temp in requests) {     totalSum += temp; } 

No, I didn't miss anything, that's all there is to it.

The foreach statement will access the array elements in a sequence equivalent to reading through the following lines.

 requests[0,0,0]  requests[0,0,1]...requests[0,0,23] requests[0,1,0]  requests[0,1,1]...requests[0,1,23] ... requests[0,6,0]   requests[0,6,1]...requests[0,6,23] requests[1,0,0]   requests[1,0,1]...requests[1,0,23] ... requests[9,0,0] requests[9,0,1]....requests[9,0,23] ... requests[9,6,0] requests[9,6,1]....requests[9,6,23]  

Note

graphics/common.gif

The foreach statement does not give any indication of which part of the array is being accessed at a particular moment. This information is sometimes vital. For example, when printing the content of an array to the screen, rows and columns often have to be printed out in accordance with how they are organized in the array. Under those circumstances, you must employ your own counters in the foreach statement to keep track of which part of the array is being accessed.


If foreach is used to access a jagged multidimensional, you cannot get away with writing just one foreach statement. You will need to nest them in a similar fashion to that of the for loops to access each dimension separately. For example, if you want to traverse the elements of jaggedRequests, defined as

 ushort [][][] jaggedRequests = new ushort [10][][]; 

you need to write

 foreach (ushort [][] outerTemp in jaggedRequests) {     foreach (ushort [] middleTemp in outerTemp)     {         foreach (ushort innerTemp in middleTemp)         {             totalSum += innerTemp;         }     } } 

which will calculate the total sum of the values of all elements in jaggedRequests.


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

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