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!

1 Like

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?

1 Like

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.

2 Likes

thanks man, I was thinking of the same thing!

What’s the code solution to the first exercise? Please share it.

You can view the Codecademy solution by clicking on “Get Unstuck”. It will bring a dropdown menu. You can then select “Solution” and finally the “Still getting stuck? Get Code Solution” option.

The first exercise is giving me hard time. I don’t know if this is on my end or on codecademy end.

First I tried this code and it’s not working.

double[][] scores = new double[][]{{80.4, 96.2, 100.0, 78.9}, {-1, -1, -1, -1}, {-1, -1, -1, -1}};

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

Second I tried this code and it’s not working too.

double[][] scores = new double[4][3];
    Arrays.stream(scores).forEach(x -> Arrays.fill(x, -1));
    scores[0] = new double[]{80.4, 96.2, 100.0, 78.9};

Third I tried this code following the hint format and it’s not working too.

double[][] scores = {{80.4, 96.2, 100.0, 78.9}, {-1, -1, -1, -1}, {-1, -1, -1, -1}};

I don’t want to click on view solution and found out the solution is the same as one of my code solution above.

None of the above solution is working. Do I need to report a bug fix to codecademy?

The first step specifies:

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). Use initializer lists to store the first exam scores in the first column and -1 for the remaining exams. Use the provided print statement to print the result in the console.

I have highlighted some phrases which should help us figure out the task at hand.
4x3 2D array means 4 rows and 3 columns i.e. 4 students and 3 exams.
In your snippets, you have created 3x4 2D array with 3 rows (students) and 4 columns(exams)

The 4 students have taken their first exam and have been graded. They still have to take the remaining two exams.

So, instead of your structure
double[][] scores = {{80.4, 96.2, 100.0, 78.9}, {-1, -1, -1, -1}, {-1, -1, -1, -1}};
the correct format should be:
double[][] scores = {{80.4, -1, -1}, {96.2, -1, -1}, {100.0, -1, -1}, {78.9, -1, -1}};

The outer-most curly brace specifies that we have an array. Each inner pair of curly braces specifies a row i.e. a student.

Thanks for your help I overthink the first part of the exercise. Now that you broke it down to pieces I overstand and get it now.

Hello Software Engineers, I need help on the fourth exercise. Here’s what I have tried!

//Using loops, copy all of the scores for exam 1 and 2 into the new 2D array. (do not include the -1 values)
  for(int i = 0; i < scores[0].length; i++) {
    for(int j = 0; j < scores.length; j++) {
      newScores[i][0] = scores[i][0];
    }
  }

Here’s the output am getting!

[[80.4, -1.0, -1.0], [96.2, -1.0, -1.0], [100.0, -1.0, -1.0], [78.9, -1.0, -1.0]]
[[80.4, 89.7, -1.0], [96.2, 90.5, -1.0], [100.0, 93.6, -1.0], [78.9, 88.1, -1.0]]
[[80.4, 0.0], [96.2, 0.0], [100.0, 0.0], [0.0, 0.0]]
[[80.4, 0.0], [96.2, 0.0], [100.0, 0.0], [0.0, 0.0]]

What am I missing in the for loop to only print the exams?
Thanks for the help.

May you help me? I need help. Am stuck on question four. I posted the code on the forum above. Thanks,

There are a number of issues with the loops you have written.

First, let us understand what we are trying to accomplish, so that we can then think about how we can accomplish the task. Originally, we have a 4x3 (4 students, 3 exams) array called scores. In steps 1 & 2, we update the array with exams scores for two exams. We then decide that there won’t be a third exam, so in step 3, we create a new 4x2 array called newScores. Since we haven’t yet entered any exam scores in this new array, so it is initialized with values of 0.0. This is the default value for a double array in which values are yet to be entered. That is why in your output, you are seeing quite a few 0.0 values.

To copy the exam scores, we need to go through every entry in the scores array EXCEPT for the last entry in each row (i.e. we don’t want the third exam score for each student). This means we must go through all the rows and then within each row, we must go through each entry except the last one. Translating this logic into code step by step will help us accomplish this task.

  • First, we want to traverse ALL the rows (students). You wrote
    for(int i = 0; i < scores[0].length; i++)
    but this doesn’t traverse all the rows. Your loop condition i < scores[0].length means that you are traversing all the columns of the first row. If you want to use Column-major order, then this loop condition may hold appeal, but even in that approach, you don’t want to traverse all the columns. You want to exclude the last column, so you would use a condition like i < scores[0].length - 1. In my explanation, I am choosing to use Row-major order, so I am going to use the loop
    for (int i = 0; i < scores.length; i++)
    This will allow us to traverse ALL the rows (i.e. all the students).

  • Now, we want to traverse all the columns in each row EXCEPT the last column. We can do so by using a loop
    for (int j = 0; j < scores[i].length - 1; j++)
    The scores[i] allows us to target the current row that we are traversing through our earlier loop. The condition j < scores[i].length - 1 means we traverse all the columns except the last column.

  • Now, we want to copy each score that we are traversing to our new array. Your code
    newScores[i][0] = scores[i][0]
    isn’t accomplishing this. Your code is just copying the first column of each row. Based on how I have written the loops, I would do the copying via a statement like this
    newScores[i][j] = scores[i][j];
    Since I have used i as the loop variable for rows and j as the loop variable for columns, so this [i][j] arrangement allows us to access and copy the correct exam score.

All told, the loop would look like:

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

The above is Row-major order. If you wanted to use a Column-major order, you could code it like:

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

You should take your time to read through the code line by line and understand each loop condition and statement for both the approaches. If you find something confusing, share your thoughts on exactly what statement is causing you trouble.