Java Reference
In-Depth Information
The SodaSurvey program computes and prints the average responses for each
soda and for each respondent. The sums of each soda and person are first stored in
one-dimensional arrays of integers. Then the averages are computed and printed.
Multidimensional Arrays
An array can have one, two, three, or even more dimensions. Any array with more
than one dimension is called a multidimensional array .
It's fairly easy to picture a two-dimensional array as a table. A three-dimensional
array could be drawn as a cube. However, once you are past three dimensions,
multidimensional arrays might seem hard to visualize. Yet, consider that each
subsequent dimension is simply a subdivision of the previous one. It is often best
to think of larger multidimensional arrays in this way.
For example, suppose we wanted to store the number of students attending
universities across the United States, broken down in a meaningful way. We
might represent it as a four-dimensional array of integers. The first dimension
represents the state. The second dimension represents the universities in each
state. The third dimension represents the colleges in each university. Finally, the
fourth dimension represents departments in each college. The value stored at
each location is the number of students in one particular department. Figure 8.5
shows these subdivisions.
Two-dimensional arrays are fairly common. However, care
should be taken when deciding to create multidimensional arrays
in a program. When dealing with large amounts of data that are
managed at multiple levels, additional information and the methods
needed to manage that information will probably be required. It is
far more likely, for instance, that in the previous example, each state
would be represented by an object, which may contain, among other things, an
array to store information about each university, and so on.
There is one other important characteristic of Java arrays to consider. As
we established previously, Java does not directly support multidimensional
KEY CONCEPT
Using an array with more than two
dimensions is rare in an object-
oriented system.
state
university
college
department
FIGURE 8.5 Visualization of a four-dimensional array
 
Search WWH ::




Custom Search