10 2D Arrays

AP Computer Science A Prep, 2024 - Rob Franek 2023

10 2D Arrays
Part V: Content Review for the AP Computer Science A Exam

2D ARRAYS

Two-dimensional, or 2D, arrays are structures that have entered, exited, and re-entered the required topics for the AP Exam. They are quite powerful and, at the same time, not too difficult to understand once you have mastered 1D array structures, which we studied in Chapter 8.

A great example of a 2D array is a stand-up soda machine, like you see in the mall or in front of the supermarket. You know the type, the one in which you drop money and hope desperately that the soda actually drops out.

Think of the soda machine as a set of rows across the machine, each having a different type of soda (let’s say that the first row has cola, second has orange, etc.). Each of those rows can be considered an array. Now consider the vertical columns down the machine; each column will have one of each type of soda, since it travels down the rows (first item is cola, second item is orange, etc.). That vertical column can also be considered an array. The result is an “array of arrays,” which Java quantifies as a 2D array, with index numbers assigned independently to each row and column location.

In the soda machine example, the very first cola in the upper left location of the 2D array would be located at index 0 of the first horizontal array, as well as index 0 of the first vertical array. The code to access this element (soda) in a 2D array (soda machine) already instantiated as sodaMachine would be sodaMachine[0][0], with the first 0 being the row and the second 0 being the column. The second cola would be located at [0][1], and the first orange would be located at [1][0], and so on. For an m by n 2D array, with m being the number of rows and n being the number of columns, the last element in the lower right corner would be located at [m - 1][n - 1]. But you didn’t forget that index numbers begin at 0 and end at (array.length - 1), did you?

The methods and constants available for use with array structures are available for 2D array structures as well, because a 2D array is an array—just a fancy one. It’s a little tricky, however; in order to, say, return the number of cola slots across the soda machine, you would use sodaMachine.length, as expected. In order to access the number of slots down the left side of the machine, however, you would use sodaMachine[0].length, meaning you want the length of the first column of the 2D array.

Image

1.Consider the following code segment:

int[][] num = new int [4][4];

for (int i = 0; i < num.length; i++)

{

for (int k = 0; k < num.length; k++

{

num[i][k] = i * k;

}

}

What are the contents of num after the code segment has executed?

(A) 0 0 0 0

0 1 2 3

0 2 4 6

0 3 6 9

(B) 0 1 2 3

1 2 3 4

2 3 4 5

3 4 5 6

(C) 0 3 6 9

0 2 4 6

0 1 2 3

0 0 0 0

(D) 1 1 1 1

2 2 2 2

3 3 3 3

4 4 4 4

(E) 0 0 0 0

1 2 3 4

2 4 6 8

3 6 9 12

Here’s How To Crack It

2D array questions are always intimidating. The first iteration of the outer loop will occur with i = 0. Looking at the inner loop, the k values will range from 0 to 3 before it terminates. Since i = 0 and each iteration of the inner loop will multiply i by k, the first row of the array will be all zeroes. Eliminate (B), (C), and (D). Wow! Once that finishes, the outer loop will have its second iteration, with i = 1. The inner loop will then start over with k = 0, and i times k will equal 0 as a result. The first number in the second row, then, will be 0. Eliminate (E) because the first number in its second row is 1: and the answer is (A).

Image

Image

Remember that an array, regardless of its dimensions, can store any one type of data, but not multiple types. Also remember that, as is true for most computer programming language rules, there is always a workaround! More on this later.

KEY TERMS

two-dimensional arrays (2D arrays)

rows

columns

CHAPTER 10 REVIEW DRILL

Answers to review questions can be found in Chapter 13.

1.The following class, Arr2d, is meant to represent a 2-dimensional array object. The constructor will initialize Arr2d using the number of rows and columns that have been passed. Choose the statement that will initialize the array in the constructor.

public class Arr2D {

private int [][] arr;

Arr2D (int rows, int columns)

{

/* missing code */

}

}

(A) int [] arr = new String [rows][columns];

(B) int [][] arr = new String [rows - 1][columns - 1];

(C) arr = new String [rows][columns];

(D) arr = new String [rows - 1][columns - 1];

(E) int arr [][] = new String [rows][columns];

2.Consider the following code segment:

int [][] numbers = {{1, 2, 3, 4}, {5, 6, 7, 8}};

for (int [] nums : numbers)

for (int n : nums)

System.out.print(n + “ ”);

System.out.print(“\n”);

What is printed as a result of executing the code segment?

(A) 1 2 3 4 5 6 7 8

(B) 1 2 3 4

5 6 7 8

(C) 1 2

3 4

5 6

7 8

(D) 5 6 7 8

1 2 3 4

(E) A compiler error would occur.

3.Consider the following code segment:

int[][] numbers = new int [4][4];

initializeIt(numbers);

int total = 0;

for (int z = 0; z < numbers.length; z++)

{

total += numbers[z][numbers[0].length - 1 - z];

}

The call to initializeIt() on the second line initializes the array numbers so that it looks like the following:

1 2 5 3

7 9 4 0

3 3 2 5

4 5 8 1

What will be the value of total after the code has executed?

(A) 11

(B) 12

(C) 13

(D) 14

(E) 15

4.Consider the following code segment:

int[][] numbers = new int[3][6];

initializeIt(numbers);

int total = 0;

for (int j = 0; j < numbers.length; j++)

{

for (int k = 0; k < numbers[0].length; k += 2)

{

total += numbers[j][k];

}

}

The call to initializeIt() on the second line initializes the array numbers so that it looks like the following:

2 4 6 3 2 1

5 6 7 4 2 9

4 0 5 6 4 2

What will be the value of total after the code has executed?

(A) 18

(B) 35

(C) 37

(D) 72

(E) 101

5.Consider the following code segment:

int[][] num = new int[4][4];

for (int i = 0; i < num.length; i++)

{

for (int k = 0; k < num[0].length; k++)

{

num[i][k] = i * k;

}

}

What are the contents of num after the code segment has executed?

(A) 0 0 0 0

0 1 2 3

0 2 4 6

0 3 6 9

(B) 0 1 2 3

1 2 3 4

2 3 4 5

3 4 5 6

(C) 0 3 6 9

0 2 4 6

0 1 2 3

0 0 0 0

(D) 1 1 1 1

2 2 2 2

3 3 3 3

4 4 4 4

(E) 0 0 0 0

1 2 3 4

2 4 6 8

3 6 9 12

6.Consider the following code segment:

int [][] numbers = {{1, 2, 3, 4},{5, 6, 7, 8}};

initializeIt(numbers);

String line =“”;

for (int a = 0; a < numbers[0].length; a++)

{

for (int b = 0; b < numbers.length; b++)

{

line += numbers[b][a] + “ ”;

}

line += “\n”;

}

System.out.println(line);

The call to initializeIt() on the second line initializes the array so that it looks like the following:

1 2 3 4

5 6 7 8

What will be printed after the code has executed?

(A) 1 2 3 4

5 6 7 8

(B) 5 6 7 8

1 2 3 4

(C) 8 7 6 5

4 3 2 1

(D) 1 2

3 4

5 6

7 8

(E) 1 5

2 6

3 4

4 8

7.Consider the following client code and method:

int [][] numbers = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

int [] myArr = mystery(numbers, 2);

for (int j: myArr)

System.out.print(j + “ ”);

public static int [] mystery(int [][] arr, int n)

{

int [] m = new int[arr.length];

for (int i = 0; i < arr.length; i++)

m[i] = arr[i][n];

return m;

}

What will be printed after the code executes?

(A) 5 6 7 8

(B) 2 6 10

(C) 9 10 11 12

(D) 4 8 12

(E) 3 7 11

8.Consider the following code:

int [][] numbers = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

for (int r = numbers.length - 1; r > 0; r——)

{

for (int c = 0; c < numbers[r].length; c++)

{

numbers[r - 1][c] = numbers[r][c];

}

}

Which of the following represents the current values in the array?

(A) 1 2 3 4

5 6 7 8

9 10 11 12

(B) 9 10 11 12

5 6 7 8

1 2 3 4

(C) 1 2 3 4

1 2 3 4

1 2 3 4

(D) 9 10 11 12

9 10 11 12

9 10 11 12

(E) 12 11 10 9

8 7 6 5

4 3 2 1

Summary

o A two-dimensional array is an array of one-dimensional arrays. On the AP Computer Science A Exam, these arrays will always be the same number of columns in each row. Also, arrays of dimensions greater than 2 are not tested.