Monday, July 24, 2023

Quiz yourself: Comparing Java arrays and interpreting the mismatch method

Quiz Yourself, Java Arrays, Oracle Java Career, Oracle Java Skills, Oracle Java Jobs, Oracle Java Prep, Oracle Java Preparation

The java.util.Arrays class methods aren’t always intuitive.

Given the following code fragment

int a1[] = { 5, 24, 54, 22 };

int a2[] = { 5, 23, 56, 202 };

int a3[] = { 3, 8, 19, 39, 56 };

System.out.print(Arrays.compare(a1, a2));

System.out.print(Arrays.compare(a3, a2));

System.out.print(Arrays.mismatch(a1, a1));

System.out.print(Arrays.mismatch(a2, a1));

Which output is guaranteed? Choose one.

A. 1-1-11

B. 2-1-12

C. 11-11

D. 2-202

E. None of the above

Answer. This question explores the Java API for the java.util.Arrays class, specifically the compare() and mismatch() methods.

The compare() method compares two arrays lexicographically. That’s a fancy way of saying it compares them in the same way that textual words are ordered in a dictionary. Importantly, that’s not the same way numbers are ordered.

Notice that for words such as Hello and Goodbye, a dictionary will order Goodbye before Hello, because G comes before H in the alphabet. Even though there are more letters in Goodbye, the first difference determines the ordering.

Contrast this with numbers; for example, 90,000 would come before 5,000,000 even though the digit 5 is less than the digit 9. The ordering imposed by Arrays.compare works this way, even when comparing arrays of numbers.

In addition, Arrays.compare checks corresponding pairs of elements at the same positions, starting at index position 0 and checking increasing index positions. As long as pairs of elements compare as equal at the same subscript, it keeps checking. If the comparison process reaches the end of both arrays simultaneously, which can happen only if no differences have been discovered, the arrays are deemed to be equal. If the comparison process reaches the end of one array before the other, but the two are identical up to the end of the shorter array, the longer array is considered to be larger.

By the way, a null array reference is considered to be smaller than any non-null array. Two null array references are considered to be equal.

In the case of this question, the elements of arrays a1 and a2 differ at index 1. At that position, the value in a1 (24) is larger than that in array a2 (23), which makes array a1 larger than array a2.

For the comparison of array a3 with array a2 (note that a3 is the first of the two arguments), the arrays differ at index 0, with array a3 being the smaller.

So, what is the result of the Arrays.compare method when the arrays have differing values at an index, as in this case? To determine that, you must go on a bit of a treasure hunt. The documentation for Arrays.compare(int[], int[]) states the following:

...the lexicographic comparison is the result of comparing two elements, as if by Integer.compare(int, int).

That leads to the documentation for Integer.compare(int, int) which states

Compares two int values numerically. The value returned is identical to what would be returned by: Integer.valueOf(x).compareTo(Integer.valueOf(y))

By following the trail, you’ll see that the documentation of Integer.compareTo(Integer) states that the result will be

...the value 0 if this Integer is equal to the argument Integer; a value less than 0 if this Integer is numerically less than the argument Integer; and a value greater than 0 if this Integer is numerically greater than the argument Integer (signed comparison).

It’s worth noting that this is consistent with the documentation for the compare method in the Comparator interface.

If you create the code for this question and try it, you’ll (almost certainly) find that the output of the comparisons is actually 1, followed by -1, which would make option A look likely to be correct (although the output from the second pair of methods has not been considered yet). But the documentation doesn’t say that the method will return 1, 0, or -1. Rather, it merely says the result will be positive, zero, or negative.

Consequently, you can’t state that the correct answer is A. Rather, the correct answer must be option E.

Although you now know the correct answer, these questions are, of course, intended for learning, rather than for merely seeing if you get the answer right or wrong. So, it’s worth investigating the behavior of the mismatch() method.

The mismatch() method returns the index value of the first point in the arrays where the values of the elements differ. If the arrays are identical in contents and length, the method returns -1. Also, if either array reference is null, a NullPointerException is thrown. (Keep in mind that Java array indexes are zero-based.)

Given these rules, the output of the mismatch operation with both arguments provided as in array a1 must be -1. The output of the mismatch operation with the arguments for array a2 and array a1 will be 1, because the index contains the values 23 and 24. This is consistent with option A.

So, if you run the code, the output will almost certainly match option A, and this would be correct if the question were phrased as “Which output is possible?” However, as noted, a valid JDK or JRE could have a library implementation that returns values other than +1 and -1 from the compare method.

We hope you won’t hate us for this question; precision and avoidance of unsound assumptions can be important in day-to-day programming as well as during exam taking!

Conclusion. The correct answer is option E.

Source: oracle.com

Related Posts

0 comments:

Post a Comment