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 Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

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 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!


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.


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}};


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?

1 Like

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.

1 Like

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.

I was wondering, why don’t I have to use the word new when declaring and initializing an array with an initializer list?

double[][] doubleValues = {{1.5, 2.6, 3.7}, {7.5, 6.4, 5.3}, {9.8,  8.7, 7.6}, {3.6, 5.7, 7.8}};

unless I reinitialize it

letters = new char[][]{{'a', 'b', 'c'}, {'d', 'e', 'f'}};

These inconsistencies are something I find hard embracing, and it holds me back a bit. Maybe, if you explained it to me, it would make it easier for me to proceed with my Java journey

I completed the first task using a for loop, like this. Too bad Codecademy doesn’t accept it unless you manually type every element into an initializer list

for(int i=0; i<scores[0].length; i++){
    for(int j=0; j<scores.length; j++){
        scores[j][i] = 80.4;
        else if(j==1){
        scores[j][i] = 96.2;
        else if(j==2){
        else {
          scores[j][i] = 78.9;
        } else {scores[j][i] = -1;

The problem with the second loop is you wrote (Row-major order) you put zero, but in the lesson appers “o”, letter o. o is a varible but you use j variable then you have to put j, like this;

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

You wrote:

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

That won’t work and an error will be thrown.
In your code, the outer loop initializes i to 0, and then checks the loop condition

i < scores[j].length - 1

to decide whether an iteration of the loop should begin. But at this point, there is no variable named j. The variable j is declared and initialized in the inner loop. Before the very first iteration of the outer loop begins, there is no variable named j and hence the condition of the outer loop can’t be evaluated.

The condition i < scores[0].length - 1 is suitable for the outer loop of the Column-major order, because the dimensions of scores is 4x3 i.e. (4 rows and 3 columns). All the rows have the same number of elements i.e. the number of columns is the same for all rows. So, scores[0].length is sufficient.

If you look at the screenshot for the explanatory text of the example in the exercise,

you can see that the condition for the column-major order is:

o < letters[0].length

as opposed to o < letters[i].length which would cause an error because i has not been declared and initialized yet.
Also, o < letters[o].length won’t be a suitable condition for the column-major order outer loop, because in the example letters is a 2x3 array (2 rows and 3 columns). Using o < letters[o].length as the condition for the outer loop will cause an "Index out of bounds" error.

Hello :slight_smile:

instead of using a row-major-loop its maybe more logical to use a colum-major-loop in this case? It was my first solution:

//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 <2 ; i++){
			for (int j = 0; j<scores.length; j++){
				newScores[j][i] = scores[j][i];

i < 2 that colum for exam nr. 3 is taking back

double scores = new double[4][3];
scores = new double{{80.4,-1,-1}, {96.2, -1, -1}, {100.0, -1, -1}, {78.9, -1, -1}};
can anyone tell whats wrong in this

the second line would just be:
scores = {{80.4,-1,-1}, {96.2, -1, -1}, {100.0, -1, -1}, {78.9, -1, -1}};
you don’t need to put the new double part twice in your code.