Skip to main content
Logo image

Section 4.13 Implementing 2D Array Algorithms

90 minutes
All of the array algorithms can be applied to 2D arrays too. There are standard algorithms that utilize 2D array traversals to:
  • determine a minimum or maximum value of all the elements or for a designated row, column, or other subsection
  • compute a sum or average of all the elements or for a designated row, column, or other subsection
  • determine if at least one element has a particular property in the entire 2D array or for a designated row, column, or other subsection
  • determine if all elements of the 2D array or a designated row, column, or other subsection have a particular property
  • determine the number of elements in the 2D array or in a designated row, column, or other subsection having a particular property
  • access all consecutive pairs of elements
  • determine the presence or absence of duplicate elements in the 2D array or in a designated row, column, or other subsection
  • shift or rotate elements in a row left or right or in a column up or down
  • reverse the order of the elements in a row or column
Remember that with 1D arrays, many algorithms followed the following patterns with a for loop or enhanced for loop.
// 1 Dimensional Array Traversal
for (int value : array)
{
    if (value ....)
        ...
}

for(int i=0; i < array.length; i++)
{
   if (array[i] ....)
       ...
}
But with 2D arrays, you will need to use nested loops to traverse the rows and columns of the 2D array. We often use indexed for loops for this to better control the row and column index values, but you can also use enhanced for loops.
// 2 Dimensional Array Traversal
for (int row = 0; row < array.length; row++)
{
    for (int col = 0; col < array[0].length; col++)
    {
        if (array[row][col] ....)
            ...
    }
}

// enhanced for loops
for (int[] row : array)
{
    for (int value : row)
    {
        if (value ....)
            ...
    }
}

Subsection 4.13.1 Sum, Average, Min, Max 2D Array Algorithms

For example, counting and searching algorithms work very similarly in 1D and 2D arrays. The following code adds all of the values in a given row.

Activity 4.13.1.

What will the following code print out? Can you complete the method called getTotalForCol that gets the total for a column? To do this, you must loop through the rows. The array’s length will tell you how many rows you have since it is an array of arrays, while the length of the array’s first element will tell you how many columns.

Activity 4.13.2.

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. Check your solution by clicking on the Check button. You will be told if any of the blocks are in the wrong order.
You can step through this code using the Java Visualizer by clicking on the following Java Visualizer
 1 
http://cscircles.cemc.uwaterloo.ca/java_visualize/#code=public+class+Test+%7B%0A+++%0A+++public+static+int+getLargest(int%5B%5D%5B%5D+arr)++%7B%0A++++int+largest+%3D+arr%5B0%5D%5B0%5D%3B%0A++++for+(int+row+%3D+0%3B+row+%3C+arr.length%3B+row%2B%2B)++%7B%0A++++++for+(int+col+%3D+0%3B+col+%3C+arr%5B0%5D.length%3B+col%2B%2B)++%7B%0A++++++++if+(arr%5Brow%5D%5Bcol%5D+%3E+largest)++%7B%0A++++++++++largest+%3D+arr%5Brow%5D%5Bcol%5D%3B%0A++++++++%7D+//+end+if%0A++++++%7D+//+end+column+loop%0A++++%7D+//+end+row+loop%0A++++return+largest%3B%0A+++%7D+//+end+method%0A+++%0A+++public+static+void+main(String%5B%5D+args)+%7B%0A++++++int%5B%5D%5B%5D+testArray+%3D+%7B%7B-32,+-6,+-3%7D,+%7B-392,+-93,+-2%7D%7D%3B%0A++++++System.out.println(getLargest(testArray))%3B%0A+++%7D%0A%7D&mode=display&curInstr=40
.

Subsection 4.13.2 Subsection of a 2D Array for a Property

You can loop through just part of a 2D array. You can change the starting value and ending value to loop through a subset of a 2D array. The following code counts the number of times a value appears in a part of the 2D array indicated by the row and column start and end values.

Activity 4.13.3.

Looping through just part of a 2D array.

Subsection 4.13.3 Duplicates in 2D Arrays

You can determine the presence or absence of duplicate elements in the 2D array or in a designated row, column, or other subsection. With 1D arrays, we sometimes needed 2 nested loops to check for duplicates or pairs. With 2D arrays, you often need 4 nested loop to check the whole array!

Activity 4.13.4.

The method noDups(nums) returns true if there are no repeated (duplicate) items in the array nums. It should return false if it does find a repeated element using nested loops.

Subsection 4.13.4 Rotate and Reverse

We can also rotate or reverse the order of the elements in a row or column. The following code rotates the elements in a row to the right by one position. This algorithm is very similar to the 1D array rotation algorithm since we are dealing with a single row or column.

Activity 4.13.5.

Create a method rotateRowRight that rotates the elements in a row to the right by one position. It should return the rotated array.

Subsection 4.13.5 Review and FRQ Practice with 2D arrays

This lesson ends the 2D arrays section of this unit. You can now do the following review lessons and FRQs at the end of the unit and College Board Progress Checks in the AP Classroom.
We encourage you to work in pairs or groups to tackle the following challenging FRQ problems and take them one step at a time. These will get easier with practice!
We also encourage you to finish the very fun Picture Lab lessons at the end of the unit in PictureLab
 11 
pictureLab.html
.
You have attempted of activities on this page.