FAQ: 2D Arrays: Java - Modifying Elements in a 2D Array

This community-built FAQ covers the “Modifying Elements in a 2D Array” 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 Modifying Elements in a 2D Array

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!

I’m not sure if maybe something just didn’t load right for me but the tictactoe example has over 80 combinations that the second and third rows could be and no insight as to what they should be so I just used the view solution and skipped past it because trying to guess the magic combination does nothing but take up time.

2D arrays keep printing a zero for the last [1][1]*5 = 0 no matter what number I put there to check. Otherwise it worked fine.

I’m a little confused. The exercise asks us to create a 2x2 array. If it starts at index zero, shouldn’t it then look like:

int subMatrix = new int[1][1];

instead of int subMatrix = new int[2][2];

1 Like

They asked for four lines of code to multiply the top left square of intMatrix by 5 and place the results in subMatrix.

Sorry, dudes, but the following is ALSO a correct answer:

for (int i = 0; i <= 1; i++) {
  subMatrix[0][i] = intMatrix[0][i] * 5;
  subMatrix[1][i] = intMatrix[1][i] * 5;
}

Even though we initialized intMatrix with 4 at the position you mentioned, in step 1 we changed that element to 0. So, in step 3, this element will evaluate to 0*5 = 0.

@dev1871983950 When we are initializing a 2D array, then the first number in brackets specifies how many rows we want. The second number specifies how many columns.
int[][] subMatrix = new int[2][2] says to create a new matrix of integers with 2 rows and 2 columns.
If we wanted to access the element in the 2nd column of the 2nd row, we would indeed use subMatrix[1][1] as the indices start from 0.
When we are trying to access/modify an element in a 2D array, then we should remember that the index starts at 0.
When we are initializing a 2D array, we aren’t specifying the index. Instead, we are specifying how many rows and columns we want.

Hi dev,

it’s like when you use .length on a String. “abc” :
index 0 = ‘a’;
index 1 = ‘b’;
index 2 = ‘c’;

but the length of the string is 3 because you have 3 char. (index != lenght).

So when they ask you to create a 2x2 array, they talking about the length but to access to it, it’s 0 and 1 like you find :slight_smile:

Hi guys,

I don’t understand why a loop (nested in my case) can be false for this exercice ? They just didn’t think about it or there is a reason ?