FAQ: 2D Arrays: Java - Traversing 2D Arrays: Introduction

This community-built FAQ covers the “Traversing 2D Arrays: Introduction” exercise from the lesson “2D Arrays: Java”.

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

FAQs on the exercise Traversing 2D Arrays: Introduction

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 () 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.

You can also find further discussion and get answers to your questions over in Language Help.

Agree with a comment or answer? 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

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!

Happy New Year everyone!
Really hoped to finish this course by the end of 2020, but made only to Learn Java[ 11 ][ 6 ] (or [ 10 ][ 5 ] when taking into account that indeces start from 0).
The third exercise:

Store the number of columns in `intMatrix` into a variable called `columns` and the number of rows in `intMatrix` into a variable called `rows` .

was a bit confusing, because I couldn’t understand why I would I do something twice (since the same thing was covered in exercises 1 and 2).
And I’m sure glad that Java wasn’t my first language to learn. There is a great deal of mindbending with all the classes, constructors, polymorphism keywords and boilerplate code.

this lesson in particular was really well fleshed out. But there is a lot of seemingly obsolete content. I reads like you try to explain the concept of nested loops 3 more times after the last lesson. I personally find it pretty easy to grasp. Maybe that is just me.
I really wished you would have put more afford like this in other more complex topics like polymorphism.

Hi all,

I am confused with intMatrix.length and intMatrix[0].length, because they both print “5”. So why are there two different formats if they are asking for the same thing? Thanks!

`intMatrix.length` and `intMatrix[0].length` do not refer to the same thing.
`intMatrix` is an array of arrays of integers.
so `intMatrix.length` is the length of the “outer” array - it tells you how many arrays are in `intMatrix`.

`intMatrix[0]` is the first array in `intMatrix` (meaning the array at an index of zero for `intMatrix`)
so `intMatrix[0].length` is the length of this “inner” array - which is how many integers there are in `intMatrix[0]`

Thanks so much for your detailed answer! It makes sense to me now!
I just realized that I made a mistake on the printout, I literally asked it to print the same thing, that’s why they both printed 5…

What are do-while loops? It seems only while loops had been covered in the course previously

Since enhanced for loops only use the element of the arrays, it is a bit more cumbersome to keep track of which index we are at. This same idea applies to while and do-while loops as well.

In a do-while loop, the expression for the condition is evaluated at the bottom. This means the do-while loop will execute at least once even if the condition is false.

A while loop checks the condition at the top. If the condition is false at the very beginning, a while loop may not execute even once.

1 Like

Hi. I’ve been struggling with step 4 for some time and still haven’t figured out why it fails:

Could you give me a hint?

Step 4 specifies:

… add a line of code which calculates the sum of all of the elements in the 2D array.

`sum++;` is just incrementing `sum` by `1`.

Instead, each element of the array needs to be added to the `sum`.

1 Like

Thank you very much!

1 Like

Hi,

i think the solution of the last exercise is 35 elements?

But what do I have to calculte for this?

sum = sum + i → 70 false,
sum = sum + intMatrix.length; → 175 false,
I have no idea to solve this

You should add each elemen, so your line of code can look this way:

sum += intMatrix[i][j];

2 Likes

Hi,
thank you so much!

I understand now also the “Hint” of codecademy: " Use `twoDArray[outerIndex][innerIndex]` to access each element"

but i forgot that sum should get a +

Without + sum is only showing the last element where i and j stopps = 79

hi i am trying to figure out how to store number of columns in intMatrix to another variable but it been hours and I could not find the answer.

As the hint for Step 3 mentions,

The number of columns is the same as the number of elements within a subarray and the number of rows is the same as the number of subarrays in a 2D array.

In Step 2, you figured out the length of the subarrays and stored it in a variable named `subArrayLength` (in your screenshot). This is the same as the number of columns in `intMatrix`, so for Step 3, you can declare and initialize a new variable as

``````int columns = subArrayLength;
``````

Similarly, in Step 1, you figured out the number of subarrays and stored it in a variable named `numSubArrays` (in your screenshot). This is the same as the number of rows in `intMatrix`, so for Step 3, you can declare and initialize a new variable as

``````int rows = numSubArrays;
``````

Before proceeding, make sure you think on why the above assignments make sense. Looking at the `intMatrix` in your screenshot, can you visualize why `numSubArrays` is the same as `rows` AND why `subArrayLength` corresponds to `columns`?