FAQ: 2D Arrays: Java - Review of Nested Loops

This community-built FAQ covers the “Review of Nested Loops” 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 Review of Nested Loops

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!

On the above code, I tried removing innerCounter = o; inside the while loop code.
Why outerCounter++ could change the variable value of int outerCounter = 0 outside while loop,
while innerCounter++ cannot change int innerCounter = 0?

Thank you.

From what I understand, innerCounter needs to be set to 0 everytime the program iterates through a new subarray because if it wasn’t, the program would only read the last element of all subarrays after that. Something similar doesn’t need to happen for outerCounter because its index must keep increasing to access each new subarray.

Here’s a diagram I drew that maps it out:

I hope I was able to clarify what you wanted to know!

3 Likes

Oh, now I see! Thank you very much!

1 Like

I thought it was a must to type parenthesis after calling a method. For example

for(int i = 0; i < seatsDayOne.length(); i++)

However, my code doesn’t work with the brackets after seatsDayOne.length, only without them. Why?

In the exercise, seatsDayOne is an array of integers. In Java, arrays don’t have a length method. Instead, arrays have a length field.

…Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.
…The array’s length is available as a final instance variable length
… The public final field length, which contains the number of components of the array. length may be positive or zero…
Source: Chapter 10. Arrays

Strings don’t have a length field. Instead, they have a length method.

length()

Returns the length of this string. The length is equal to the number of Unicode code units in the string.
Source: String (Java Platform SE 7 )

int[] arr = new int[5];
System.out.println(arr.length); // Output: 5
System.out.println(arr.length()); // ERROR. Arrays don't have length method.
arr = new int[] {4, 6, 8}; // Assigning new array to arr
System.out.println(arr.length); // Output: 3
        
String myStr = "abcdefg";
System.out.println(myStr.length()); // Output: 7
System.out.println(myStr.length); // ERROR. Strings don't have length field/property.

Related:

1 Like

Thank you! Usually, instance fields are declared and initialized. So, if I understood you correctly, you can have invisible instance fields in java, don’t you?

1 Like

I don’t quite understand what you mean by “invisible”.

If we simply declare an array without initialization, then we haven’t created an array yet or allocated memory. When we specify the size of the array or use an Array literal, then memory is allocated and array is created. At this time, length is assigned a value which can’t be changed.

// Declaration without initialization. No array created or memory allocated.
// x.length will throw error
int[] x; 

// Size explicitly specified
int[] y = new int[8];
// y.length will be 8

// Size inferred by compiler
int[] z = {5, 6, 7, 8};
// z.length will be 4

2 Likes

What I meant by “invisible” is you don’t declare and initialize it as usual, like

int length = 8;

If I understand it correctly, you’re referring to the fact that we’ve learned that instance fields can be manually declared in java.

And now this is confusing because it suddenly does it for you?

Well, here we are using the length variable. When we create an array, it instantly creates this instance field and sets it to a final value for us as the length of an array cannot be changed.

Therefore, you do not have to declare the length yourself, and you can simply use it whenever you want to check the length of your array.

Hope this helped you.

1 Like

very confused on the last part of this exercise. Can someone explain in simple terms?

Each of the 2 arrays has a seat number [ i ] and a reg number [ j ].
We are comparing 2 arrays to see if the reg number shows up twice.

However the conditional logic statement solution is this…

if(seatsDayOne[i]==seatsDayTwo[j])

I would think we want to compare apples to apples (so speak) so why would the right solution not be comparing ‘j’ for each array?

if(seatsDayOne[j]==seatsDayTwo[j])

NOt getting this. Any clear explanation is appreciated.

In short, [j] belongs to arrayseatsDayTwo.

Long answer:
seatsDayOne[i] and seatsDayTwo[j] are the contestants’ IDs, e.g. 850007, 841141.
[i] and [j] are the seat numbers, on Day 1 (arrayseatsDayOne) and Day 2 (arrayseatsDayTwo), respectively.

The conditional logic of: seatsDayOne[i]==seatsDayTwo[j] is correct because as we go along the seat number on both days (computed by the two ‘for’ loops above the ‘if’), we are checking if there are any contestant IDs that exist on both days.

So, if the code finds the same ID, it will add the count to matchCounter.

Thank you very much I get it now, i was confusing with 2d array using i as j for inner outer loops. This is comparing 2 separate 1d arrays. Seat no is index. Thanks again!

Just for the record, this is what they want to see:

1 2 3 4 5 6 7 8 9 10 
2 4 6 8 10 12 14 16 18 20 
3 6 9 12 15 18 21 24 27 30 
4 8 12 16 20 24 28 32 36 40 
5 10 15 20 25 30 35 40 45 50 
6 12 18 24 30 36 42 48 54 60 
7 14 21 28 35 42 49 56 63 70 
8 16 24 32 40 48 56 64 72 80 
9 18 27 36 45 54 63 72 81 90 
10 20 30 40 50 60 70 80 90 100