|
|
|
|
|
|
|
It is possible to have arrays of more than one dimension. Each dimension is represented as a subscript in the array. Therefore, a two-dimensional array has two subscripts, a three-dimensional array has three subscripts, and so on. Arrays can have any number of dimensions, although it is likely that most of the arrays you create will have one or two dimensions. |
|
|
|
|
|
|
|
|
A good example of a two-dimensional array is a chess board. One dimension represents the eight rows; the other dimension represents the eight columns. Figure 15.3 illustrates this idea. |
|
|
|
|
|
|
|
|
FIGURE 15.3
A chess board and a two-dimensional array. |
|
|
|
|
|
|
|
|
Suppose that you have a class named SQUARE. The declaration of an array named Board that represents it would be |
|
|
|
|
|
|
|
|
You could also represent the same data with a one-dimensional, 64-square array. For example: |
|
|
|
|
|
|
|
|
This doesn't correspond as closely to the real-world object as the two-dimensional array, however. When the game begins, the king is located in the fourth position in the first row. Counting from zero, that position corresponds to |
|
|
|
|
|
|
|
|
assuming that the first subscript corresponds to row, and the second to column. The layout of positions for the entire board is illustrated in Figure 15.3. |
|
|
|
|
|