FAQ: 2D Arrays: Java - Combining Traversal and Conditional Logic

This community-built FAQ covers the “Combining Traversal and Conditional Logic” exercise from the lesson “2D Arrays: Java”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Combining Traversal and Conditional Logic

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi!

Why do I get the following error message on the console when I don’t try to ‘convert’ anything?

Combining.java:21: error: incompatible types: int cannot be converted to int
for(int image: newImage){
^
1 error

My code block is:

for(int image: newImage){
if(image - 50 < 0){
image = 0;
}
else {
image -= 50;
}
}

newImage would be a 2D array of ints (meaning newImage is an array of arrays of integers);
so the stuff inside newImage are arrays.
In the code posted, you tried to subtract 50 from an array, which creates an error.

To fix that, you can have an outer loop that iterates through the arrays of integers, and an inner loop that iterates through the integers.

for(int[] row: newImage){
  for (int image: row) {
    if(image - 50 < 0){
      image = 0;
    }
    else {
      image -= 50;
    }
  }
}

Hi, I did just that and the correct values for the modified newImage 2D Array still don’t print to the console.

It doesn’t throw an error, however. It simply doesn’t print the new values, it prints the same old values twice.

Thank you. :slight_smile:

Yea, I think that inner lop made a copy of the values, and changed that copy instead of changing the original integers.
I guess the inner loop should iterate using the index instead.

    for(int[] row: newImage){
      for (int j = 0; j < row.length; j++) {
        if(row[j] - 50 < 0){
          row[j] = 0;
        }
        else {
          row[j] -= 50;
        }
      }
    }

Could you tell me please why it doesn’t work? I know the task could be achieved simpler, but I wanted to use the Math.abs() method. I imported the library correctly (import static java.lang.Math.*; )

for(i = 0; i < newImage.length; i++){
      j = 0;
      for(j = 0; j < newImage[i].length; j++){
        newImage[i][j] -= 50;
        if(newImage[i][j] < 0){
          newImage[i][j] += Math.abs(newImage[i][j]);
        }
        }
      }

Here the solution:

1 Like

Single nested for loop solution for steps 2 & 3. It seems like it might be easy to miss that, having already defined the parameters of the newImage 2D array, performing math or having an extra conditional in the for loop using the bounds of the imageData 2D array is unnecessary.

Instead of iterating through the 8 columns of imageData and using a conditional to ignore the last two, we can use the defined 6 rows of the newImage 2D array to automatically stop after six items when copying from each sub-array.

And since we are already iterating through, we can alter each value of the inner array as we copy it over to the newImage

for(int i = 0; i < newImage.length; i++) {
      for(int j = 0; j < newImage[i].length; j++) {
        if(imageData[i][j] > 49) {
          newImage[i][j] = imageData[i][j] - 50;
        } else {
          newImage[i][j] = 0;
        }
      }
    }

import java.util.Arrays;
public class Combining {
public static void main(String args) {
int imageData={{100,90,255,80,70,255,60,50},
{255,10,5,255,10,5,255,255},
{255,255,255,0,255,255,255,75},
{255,60,30,0,30,60,255,255}};

	//First, we want to crop the image down to a 4x6 image, removing the right 2 columns. Declare and initialize a new 2D array of integers with 4 rows and 6 columns called `newImage`.
	int[][] newImage = new int[4][6];
	
for (int i=0; i<imageData[0].length && i<6; i++) {
  for (int j =0; j<imageData.length; j++) {
    newImage[j][i] = imageData[j][i];
  }
}


	//Now that you have your empty image, use nested **for** loops to copy over the data from the original image to the new image, make sure not to include the cropped out columns.


	System.out.println(Arrays.deepToString(newImage));

	//You want to decrease the brightness of the new image by 50 units. The way this works is that for every integer in the new 2D array, we will subtract the value by 50. Remember that the value range for the pixel is 0-255, so if the result tries to go below 0, just set it equal to 0.
	for (int[] row: newImage) {
  for (int j=0; j<row.length; j++) {
    if (row[j] - 50 < 0 ) {
      row[j] = 0;
    } else {
      row [j] -= 50;
    }
  }
}

	
	System.out.println(Arrays.deepToString(newImage));
}

}

import java.util.Arrays;
public class Main {
	public static void main(String[] args) {
		int[][] imageData={
      {100,90,255,80,70,255,60,50},
			{255,10,5,255,10,5,255,255},
			{255,255,255,0,255,255,255,75},
			{255,60,30,0,30,60,255,255}
    };
		// Declare and initialize the 2D array newImage
		int[][] newImage = new int[4][6];

		// Add a nested `for` loop and copy the data of `imagedata` to `newImage`

    for(int i=0; i<newImage.length; i++) {
      for(int j=0; j<newImage[i].length; j++){
        newImage[i][j] = imageData[i][j];
      }
    }
		System.out.println(Arrays.deepToString(newImage));
			
		for(int i=0; i<newImage.length; i++){
      for(int j=0; j<newImage[i].length; j++){
        // Add the if-else statement here
        if(newImage[i][j] -50 < 0){
          newImage[i][j] = 0;
        }else{
          newImage[i][j] -= 50;
        }
      }
    }
		System.out.println(Arrays.deepToString(newImage));
	}
}