|
|
|
|
|
|
|
SomeArray[0][0]: 0
SomeArray[0][1]: 0
SomeArray[1][0]: 1
SomeArray[1][1]: 2
SomeArray[2][0]: 2
SomeArray[2][1]: 4
SomeArray[3][0]: 3
SomeArray[3][1]: 6
SomeArray[4][0]: 4
SomeArray[4][1]: 8 |
|
|
|
|
|
|
|
|
Analysis: Line 4 declares SomeArray to be a two-dimensional array. The first dimension consists of five integers; the second dimension consists of two integers. This creates a 5×2 grid, as Figure 15.4 shows. |
|
|
|
|
|
|
|
|
The values are initialized in pairs, although they could be computed as well. Lines 5 and 6 create a nested for loop. The outer for loop ticks through each member of the first dimension. For every member in that dimension, the inner for loop ticks through each member of the second dimension. This is consistent with the printout; SomeArray[0][0] is followed by SomeArray[0][1]. The first dimension is incremented only after the second dimension is incremented by 1. Then the second dimension starts over. |
|
|
|
|
|
|
|
|
When you declare an array, you tell the compiler exactly how many objects you expect to store in it. The compiler sets aside memory for all the objects, even if you never need to use it. This isn't a problem with arrays for which you can estimate how many objects you'll need. For example, a chess board has 64 squares, and CATs have between 1 and 10 kittens. When you have no idea of how many objects you'll need, however, you must use more advanced data structures. |
|
|
|
|
|
|
|
|
This book looks at arrays of pointers, arrays built on the free store, and various other collections. More advanced data structures that solve large data-storage problems are beyond the scope of this book. Two great things about programming are that there are always more things to learn and that there are always more books from which to learn. |
|
|
|
|
|