traverse 2D arrays using nested enhanced for loops
In this lesson, you will learn how to use nested loops to traverse a 2D Array.
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 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 exam all 2D 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.
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 traverse through all of the elements of a 2D array:
The following code has a method called getAverage which uses nested for loops to find the total of the values in the array called matrix.
Notice that an array can be passed in as an argument to a method:
Some key things to notice about the code above are:
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 (truncated).
The array called matrix 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.
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 this:
ENHANCED FOR-EACH LOOP FOR 2D ARRAYS
Since 2D arrays are really arrays of arrays you can also use a nested for-each loop to loop through all elements in an array.
We loop through each of the inner arrays and loop through all the values in each inner array.
Notice the type of the outer loop array variable; it is an array that will hold each row!
In this case the for (int[] colArray : a) means to loop through each element of the outer array which will set colArray to the current column array.
Then you can loop through the value in the column array.
2D ARRAY ALGORITHMS
All of the array algorithms can be applied to 2D arrays too.
For example, counting and searching algorithms work very similarly.
The following code adds all of the values in a given row AND the getTotalForCol method gets the total for a column.
The getTotalForCol method loops through the rows; the array's length will is the number of rows (because it is an array of arrays), and the length of the array's first element gives the number of columns:
As we have seen before in previous lessons it is also possible to loop through just part of a 2D array.
The starting and ending values can be changed to loop through a subset of a 2D array:
Here is a linear search algorithm where we access each row and then apply a linear search on it to find an element:
And finally I changed the code above to work for a String 2D array instead of an int array.
Notice that the indices row and col still need to be ints:
SUMMARY
We can loop through 2D arrays using nested for loops or nested enhanced for each loops.
The outer loop for a 2D array usually traverses the rows, while the inner loop traverses the columns in a single row.
The 2D array's length gives the number of rows. A row's length array[0].length gives the number of columns.
Nested iteration statements can be written to traverse the 2D array in "row-major order" or "column-major order."
In an enhanced for each loop, the variable of the outer loop must be the type of each row, which is a 1D array. The inner enhanced for loop variable must be the same type as the elements stored in the array.
All standard 1D array algorithms can be applied to 2D array objects.
When applying sequential/linear search algorithms to 2D arrays, each row must be accessed then sequential/linear search applied to each row of a 2D array.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.