Two-Dimensional Arrays

Chapter 11

Two-Dimensional Arrays

This chapter introduces Java arrays with two subscripts for managing data logically stored in a table-like

format in rows and columns. This structure proves useful for storing and managing data in many

applications, such as electronic spreadsheets, games, topographical maps, and student record books.

11.1

2-D Arrays

Data that conveniently presents itself in tabular format can be represented using an array with two subscripts,

known as a two-dimensional array. Two-dimensional arrays are constructed with two pairs of square brackets

to indicate two subscripts representing the row and column of the element.

Chapter 11: Tw0-Dimensional Arrays

General Form: A two-dimensional array consruction (all elements set to default values)

type[][] array-name = new type [row-capacity][column-capacity];

type[][] array-name = { { element[0][0], element[0][1], element[0][2], ¡­ } ,

{ element[1][0], element[1][1], element[1][2], ¡­ } ,

{ element[2][0], element[2][1], element[2][2], ¡­ } };

type may be one of the primitive types or the name of any Java class or interface

identifier is the name of the two-dimensional array

rows specifies the total number of rows

columns specifies the total number of columns

Examples:

double[][] matrix = new double[4][8];

// Construct with integer expressions

int rows = 5;

int columns = 10;

String[][] name = new String[rows][columns];

// You can use athis shortcut that initializes all elements

int[][] t = { { 1, 2, 3 },

// First row of 3 integers

{ 4, 5, 6 },

// Row index 1 with 3 columns

{ 7, 8, 9 } }; // Row index 2 with 3 columns

Referencing Individual Items with Two Subscripts

A reference to an individual element of a two-dimensional array requires two subscripts. By convention,

programmers use the first subscript for the rows, and the second for the columns. Each subscript must be

bracketed individually.

General Form: Accessing individual two-dimensional array elements

two-dimensional-array-name[rows][columns]

rows is an integer value in the range of 0 through the number of rows - 1

columns is an integer value in the range of 0 through the number of columns - 1

Examples:

String[][] name = new String[5][10];

name[0][0] = "Upper Left";

name[4][9] = "Lower Right";

assertEquals("Upper Left", name[0][0]);

// name.length is the number of rows,

// name[0].length is the number of columns

assertEquals("Lower Right", name[name.length-1][name[0].length-1]);

Nested Looping with Two-Dimensional Arrays

Nested looping is commonly used to process the elements of two-dimensional arrays. This initialization

allocates enough memory to store 40 floating-point numbers¡ªa two-dimensional array with five rows and

eight columns. Java initializes all values to 0.0 when constructed.

int ROWS = 5;

int COLUMNS = 8;

double[][] table = new double[ROWS][COLUMNS];

// 40 elements set to 0.0

These nested for loops initialize all 40 elements to -1.0.

// Initialize all elements to -1.0

for (int row = 0; row < ROWS; row++) {

for (int col = 0; col < COLUMNS; col++) {

table[row][col] = -1.0;

}

}

Self-Check

Use this construction of a 2-D array object to answer questions 1 through 8:

int[][] a = new int[3][4];

11-1

11-2

11-3

11-4

11-5

11-6

11-7

11-8

What is the value of a[1][2]?

Does Java check the range of the subscripts when referencing the elements of a?

How many ints are properly stored by a?

What is the row (first) subscript range for a?

What is the column (second) subscript range for a?

Write code to initialize all of the elements of a to 999.

Declare a two-dimensional array sales such that stores 120 doubles in 10 rows.

Declare a two-dimensional array named sales2 such that 120 floating-point numbers can be stored

in 10 columns.

A two-dimensional array manages tabular data that is typically processed by row, by column, or in totality.

These forms of processing are examined in an example class that manages a grade book. The data could look

like this with six quizzes for each of the nine students.

0

1

2

3

4

5

6

7

8

Quiz #0

67.8

76.4

87.8

86.4

72.8

94.4

85.8

76.4

57.9

1

56.4

81.1

76.4

54.0

89.0

63.0

95.0

84.4

49.5

2

88.4

72.2

88.7

40.0

55.0

92.9

88.1

100.0

58.8

3

79.1

76.0

83.0

3.0

62.0

45.0

100.0

94.3

67.4

4

90.0

85.6

76.3

2.0

68.0

75.6

60.0

75.6

80.0

5

66.0

85.0

87.0

1.0

77.7

99.5

85.8

74.0

56.0

This data will be stored in a tabular form as a 2D array. The 2D array will be processed in three ways:

1. Find the average quiz score for any of the 9 students

2. Find the range of quiz scores for any of the 5 quizzes

3. Find the overall average of all quiz scores

Here are the methods that will be tested and implemented on the next few pages:

// Return the number of students in the data (#rows)

public int getNumberOfStudents()

// Return the number of quizzes in the data (#columns)

public int getNumberOfQuizzes()

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download