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.
Ask or answer a question about this exercise by clicking 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 () to up-vote the contribution!
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?
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.
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 publicfinal 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.
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?
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
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.
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!