Chapter 7.5.2 – Using Two dimensional Arrays | Introduction to Programming Using Java

Chapter 7.5.2 – Using Two dimensional Arrays | Introduction to Programming Using Java

 

7.5.2 Using Two dimensional Arrays

 

Just as in the case of one-dimensional arrays, two-dimensional arrays are often processed using for statements. To process all the items in a two-dimensional array, you have to use one for statement nested inside another. If the array A is declared as int[][] A = new int[3][4];

then you could store a zero into each location in A with:

 

55 7 Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

The first time the outer for loop executes (with row = 0), the inner for loop fills in the four values in the first row of A, namely A[0][0] = 0, A[0][1] = 0, A[0][2] = 0, and A[0][3] = 0. The next execution of the outer for loop fills in the second row of A. And the third and final execution of the outer loop fills in the final row of A.

Similarly, you could add up all the items in A with:

 

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

This could even be done with nested for-each loops. Keep in mind that the elements in A are objects of type int[ ], while the elements in each row of A are of type int:

 

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

To process a three-dimensional array, you would, of course, use triply nested for loops.

A two-dimensional array can be used whenever the data that is being represented can be arranged into rows and columns in a natural way. Often, the grid is built into the problem. For example, a chess board is a grid with 8 rows and 8 columns. If a class named ChessPiece is available to represent individual chess pieces, then the contents of a chess board could be represented by a two-dimensional array:

 

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

Or consider the “mosaic” of colored rectangles used in an example in Subsection 4.6.2. The mosaic is implemented by a class named MosaicCanvas.java. The data about the color of each of the rectangles in the mosaic is stored in an instance variable named grid of type Color[ ][ ]. Each position in this grid is occupied by a value of type Color. There is one position in the grid for each colored rectangle in the mosaic. The actual two-dimensional array is created by the statement:

 

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

where ROWS is the number of rows of rectangles in the mosaic and COLUMNS is the number of columns. The value of the Color variable grid[i][j] is the color of the rectangle in row number i and column number j. When the color of that rectangle is changed to some color, c, the value stored in grid[i][j] is changed with a statement of the form “grid[i][j] = c;”. When the mosaic is redrawn, the values stored in the two-dimensional array are used to decide what color to make each rectangle.

Here is a simplified version of the code from the MosaicCanvas class that draws all the colored rectangles in the grid. You can see how it uses the array:

 

60 Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

Sometimes two-dimensional arrays are used in problems in which the grid is not so visually obvious. Consider a company that owns 25 stores. Suppose that the company has data about the profit earned at each store for each month in the year 2006. If the stores are numbered from 0 to 24, and if the twelve months from January ’06 through December ’06 are numbered from 0 to 11, then the profit data could be stored in an array, profit, constructed as follows:

 

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

profit[3][2] would be the amount of profit earned at store number 3 in March, and more generally, profit[storeNum][monthNum] would be the amount of profit earned in store number storeNum in month number monthNum. In this example, the one-dimensional array profit[storeNum] has a very useful meaning: It is just the profit data for one particular store for the whole year.

Let’s assume that the profit array has already been filled with data. This data can be processed in a lot of interesting ways. For example, the total profit for the company—for the whole year from all its stores—can be calculated by adding up all the entries in the array:

 

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

Sometimes it is necessary to process a single row or a single column of an array, not the entire array. For example, to compute the total profit earned by the company in December, that is, in month number 11, you could use the loop:

 

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

Let’s extend this idea to create a one-dimensional array that contains the total profit for each month of the year:

 

64 Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

As a final example of processing the profit array, suppose that we wanted to know which store generated the most profit over the course of the year. To do this, we have to add up the monthly profits for each store. In array terms, this means that we want to find the sum of each row in the array. As we do this, we need to keep track of which row produces the largest total.

 

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

Chapter 7.5.2 - Using Two dimensional Arrays | Introduction to Programming Using Java

 

 

 

Read More…

 

1 thought on “Chapter 7.5.2 – Using Two dimensional Arrays | Introduction to Programming Using Java”

Leave a Comment