Dylan Kwon wrote:1. Let's say I want a Comparator that sorts the longest strings first and there are no nulls values.
I am told this is the correct method for doing so, but doesn't this method work as well?:
Well, it is a bit confusing how it is asked, but the problem basically is to sort strings based on their lenghts in either:
a. ascending order (shortest to longest)
b. descending order (longest to shortest)
First variant would achieve (a), while second variant would achieve (b). So that's the difference between the two implementations.
Why is like so? Look at Comparator interface
documentation. It says:
Parameters:
o1 - the first object to be compared.
o2 - the second object to be compared.
Returns:
a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
So let's say first string
s1 (or o1 as it's called in docs) is of length 5 and the second string
s2 is of length 10. Obviously there is a difference whether you subtract 10 from 5 or 5 from 10. You get either -5 or +5 respectively.
Based on explanation in
Returns part, it is explained, how depending on the expression result is defined which element is smaller, greater or they both are equal.
Dylan Kwon wrote:2.
In a 2D array, letters does "letters[0]" refer to the 1st row?
People normally think about rows and columns when they trying to visualise an array of arrays. But really there is nothing about rows or columns.
So you know, it is comfortable to think as about rows and columns when you speak with someone, where i.e. A[0] is a first row, and A[1][1] would refer to a number 5, which is row 2 and column 2, but that's only because I formatted this array of arrays that way. I could have just formatted it simply that way:
Now we have what? Just a one row from its visual looks.
So it is an array of arrays. A[0] is referring to a very first array within an array. It could be more complicated, an array of arrays, that each array's element is an another array, then another... How you'd represent rows then? Probably that would be representation with more dimensions, but again, that's just model how you visualise in your head.