# 8.2.4. Enhanced For-Each Loop for 2D Arrays (Day 2)¶

Since 2D arrays are really arrays of arrays you can also use a nested enhanced 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!

Nested enhanced for loops demo.

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.

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

# 8.2.5. 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.

Coding Exercise

What will the following code print out? Can you add another method that gets the total for a column?

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.

Loooping through just part 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.

Coding Exercise

What will the following code print? Can you change the code to work for a String 2D array?

# 8.2.6. Programming Challenge : Picture Lab¶

Photographs and images are made up of a 2D array of pixels which are tiny picture elements that color in the image. The color of a pixel is represented using the RGB (Red, Green, Blue) color model, which stores values for red, green, and blue, each ranging from 0 to 255. You can make any color by mixing these values! Try the RGB Color Mixer to experiment. Can you make black? Can you make white? Can you make purple?

Scroll down to the bottom of the following code and take a look at the switchColors method. Run the code and watch what it does. It switches RGB values of each pixel and the colors change!

Picture Lab

Now, write a similar method called zeroBlue() that sets the blue values at all pixels to zero. You can test it in the active code above or on this Repl.it Swing project or this alternative Repl.it project by teacher Jason Stark from LA (click output.jpg to see the result) or your own IDE to see what it does.

Here are some more exercises from the Picture Lab:

• Write a method keepOnlyBlue that will keep only the blue values, that is, it will set the red and green values to zero.

• Write a negate method to negate all the pixels in a picture. To negate a picture, set the red value to 255 minus the current red value, the green value to 255 minus the current green value and the blue value to 255 minus the current blue value.

• Write the grayscale method to turn the picture into shades of gray. Set the red, green, and blue values to the average of the current red, green, and blue values (add all three values and divide by 3).

You can continue on with the Picture Lab to mirror images and create collages and detect edges as the first step in recognizing objects in images.

# 8.2.7. 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 a 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.

You have attempted of activities on this page