Multi-Dimensional Arrays


The arrays we have looked at so far have been one-dimensional. This means that each component is specified by a single unique index. Java also supports multi-dimensional arrays, in which each component is specified by a unique sequence of indices. The number of indices in the sequence is the array's dimension. In a two-dimensional array, for example, each component has two indices. Figures 6.1 and 6.2 portrayed some one-dimensional arrays as columns of boxes. We can portray a two-dimensional array as a lattice or matrix, where each component is identified by its row and column, as in Figure 6.3.


Figure 6.3: A two-dimensional array

If the array in Figure 6.3 were called twoDimInts, it would be declared as

int[][]   twoDimInts; 

The array is now declared but has not been created yet. When you create an n-dimensional array, you have to specify n sizes: one size for each dimension. To create a two-dimensional array of 3 rows and 2 columns, as in Figure 6.3, use the following code:

twoDimInts = new int[3][2];

Now the array has been created. At creation time, every component is initialized, as with one-dimensional arrays. To access an individual component, you use the array name followed by both indices, with each index in square brackets. For example, the following code initializes every component of twoDimInts to 39:

  for (int i=0; i<3; i++)     for (int j=0; j<2; j++)       twoDimInts[i][j] = 39;

Suppose you have 50 weather stations, each of which takes a temperature reading every hour throughout one day. You might store the data in a two-dimensional float array called temps, where temps[t][s] is the temperature at time t recorded by station s.

The following code could be used to print the average temperature over all stations, hour by hour:

for (int hour=0; hour<24; hour++) {   float tempTotal = 0;   for (int stn=0; stn<50; stn++)     tempTotal += temps[hour][stn];   float tempAvg = tempTotal / 50;   System.out.println("Average temp at time " + hour +                      " = " + tempAvg); }

On the other hand, you might want the average temperature over the entire day for each station. For that, you would use the following code:

for (int stn=0; stn<50; stn++) {   float tempTotal = 0;   for (int hour=0; hour <24; hour++)     tempTotal += temps[hour][stn];   float tempAvg = tempTotal / 24;   System.out.println("Average temp at station " + stn +                      " = " + tempAvg); }

These examples show that processing a two-dimensional array generally requires a two-deep nested loop. In general, processing an N-dimensional array requires an N-deep nested loop.

The BoolArrayLab animated illustration uses a two-deep nested loop to let you set the values in a 200-by-200 boolean array. The array contents are illustrated by a grid of 200 by 200 pixels. A blue pixel represents a value of true; a black pixel represents false. (You probably can't see the individual pixels unless you use a magnifying glass.) To run the program, type java arrays.BoolArrayLab. The code looks like this:

boolean[][] bools = new boolean[200][200]; for (int y=0; y<200; y++)   for (int x=0; x<200; x++)     bools[x][y] = ________________________ ;

You supply the formula in the last line. The formula can be any valid boolean expression. Initially, the program comes up with the following formula:

    bools[x][y] = x>y;

Figure 6.4 shows BoolArrayLab's initial screen.

click to expand
Figure 6.4: BoolArrayLab

The File ‚ Gallery menu offers 7 sample formulas, and you are encouraged to try your own or modify the ones provided. Figure 6.5 shows a parabola.

click to expand
Figure 6.5: BoolArrayLab drawing a parabola

The lower portion of the display renders the contents of the array. Since 200 x 200 is fairly large, the display uses a rectangular grid of 200 x 200 pixels. An array component with a value of true is represented by a blue pixel, while a false value is represented by a black pixel.

Before you launch the program, can you guess what sort of image is produced by the initial formula bools[x][y] = x>y;? If you discover a formula that produces an interesting display, please email it to www.sybex.com. I will use it and mention your name in the next edition.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

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