# 8.2.1. Nested Loops for 2D Arrays (Day 1)¶

In this lesson, you will learn how to use nested loops to traverse a 2D Array.

# 8.2.2. Getting the Number of Rows and Columns¶

Arrays know their length (how many elements they can store). The length is a public read-only field so you can use dot-notation to access the field (arrayName.length). The length of the outer array is the number of rows and the length of one of the inner arrays is the number of columns.

ticketInfo.length // returns the number of rows
ticketInfo[0].length // returns the number of columns


Note

Note that length is a field and not a method, so you don’t add parentheses after length. However, if you use parentheses after length during the exam, you won’t lose any points. Since for the AP CS A exam all two-dimensional arrays are rectangular arrays (arrays that have the same number of columns in each row) you can just use the length of the first inner array as the number of columns as shown by ticketInfo[0].length.

# 8.2.3. Looping Through a 2D Array¶

Since you can find out the number of rows and columns in a 2D array you can use a nested for loop (one loop inside of another loop) to loop/traverse through all of the elements of a 2D array.

int[][] array = { {1,2,3},{4,5,6}};
for (int row = 0; row < array.length; row++)
{
for (int col = 0; col < array[0].length; col++)
{
System.out.println( array[row][col] );
}
}


Coding Exercise

What does the following code do? Add another row of numbers to the matrix. Will the loops traverse this row too? Use the CodeLens button to trace through the code. Note that an array can be passed in as an argument to a method.

• total is declared to be a double so that the result will be a double. If total was declared to be an int then the result would be an integer and the values after the decimal point would be thrown away.

• The array is passed in as an argument to the method.

• The number of rows is a.length

• The number of columns is a[0].length

• The number of times this loop executes is the number of rows times the number of columns.

Mixed up programs

The following has the correct code to find the largest value in a 2D array. Drag the blocks from the left into the correct order on the right and indent them as well. Check your solution by clicking on the Check button. You will be told if any of the blocks are in the wrong order or have the wrong indention.

You can step through this code using the Java Visualizer by clicking on the following Java Visualizer.

Most nested loops with 2D Arrays use “row-major order” where the outer loop goes through each row. However, you can write nested loops that traverse in “column-major order” like below.

Coding Exercise

What will the following code print out? Try to guess before you run it.

# 8.2.4. AP Practice¶

This lesson is continued on the next page.