FAQ: 2D Arrays: Java - 2D Array Review

This community-built FAQ covers the “2D Array Review” 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 2D Array Review

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!

I have a question regarding the final section of the review exercise: For all exam grades less than 90, add 2 additional points to the grade in newScores.

I have solved it using the following code, which works fine:

for(int i = 0; i < newScores.length; i += 1){

    for(int j = 0; j < newScores[i].length; j += 1){

      if (newScores[i][j] < 90){

        newScores[i][j] += 2;

      }
    }
  }

However, initially, my code was this:

for(double[] scoreSet : newScores){

    for(double score : scoreSet){

      if (score < 90){

        score += 2;

      }
    }
  }

I could use the latter code to access the array values (for example by printing them), however, I couldn’t modify them. Why is that?

My thoughts are is that it is because in order to modify the array value, I need to access it’s location, rather than the value itself. Is that right?

Thank you!

I had some trouble with exercise. I’m not sure if it was just on my end or not so I’ve submitted a bug report. In case anyone gets the same issue:
I found the answer to the first question wasn’t accepted. I checked “view solution” and my answer was right. I even tried copying the code directly from “view solution” and it wasn’t working. When I tried to reset my progress I was told “Review.java” was missing, but the filename actually in use was “Exercise9.java”. The only way I could progress was to rename the file (both the actual filename and in the code) to “Review”.

I also have a question, as this threw me off a bit:

First, declare and initialize a 4x3 2D array of doubles called scores which will contain the exam data for four students. The rows will represent the student and the columns will represent the exam number. You already know the first exam scores (80.4, 96.2, 100.0, 78.9).

So we have four rows and three columns. Thinking of this as a table, it’s 3x4, not 4x3. I understand you’d initialise an empty array with [4][3] but I’d still think of this as a 3x4 array if using rows and columns. Is it just a quirk of coding that the Y value comes first when talking about dimensions?

If we just want to print the elements or do some calculations, then enhanced for loops work really well because we don’t have to bother with the indices. But if we want to change the values in the array, then as you figured out, we need the location. So, if we want to modify the values, then a for loop iterating over the indices would be a better choice than an enhanced for loop.
In your second loop for (double score : scoreSet), each element in the scoreSet array gets assigned to score as we iterate over the array. But score only contains the value of the element not the location. So score += 2; changes the value contained in score but doesn’t change the value contained in our original array.