Page 1 of 1

About Question enthuware.ocpjp.v8.2.1307 :

Posted: Sun Sep 27, 2015 7:31 pm
by nelsonct
We have the array

Code: Select all

 static String[] sa = { "d", "bbb", "aaaa" }; 
Why is the answer "Arrays.binarySearch(sa, "c", new MyStringComparator()); will return 0." correct? To my understanding, "c" does not exist and will be inserted at index 0, thus Arrays.binarySearch(sa, "c", new MyStringComparator()); will return -(0)-1 = -1. However, that answer was marked wrong.

Re: About Question enthuware.ocpjp.v8.2.1307 :

Posted: Sun Sep 27, 2015 8:38 pm
by admin
Why do you think "c" will be inserted at index 0? The explanation tells exactly where it will be inserted: "The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key,..."

Re: About Question enthuware.ocpjp.v8.2.1307 :

Posted: Wed May 10, 2017 9:20 am
by Elecded
binarySearch would return 0 if "c" would be found in array at index 0, right?

The first element greater than "c" is "bbb" with index 1. Then binarySearch should return -(1+1) = -2.

<EDIT> The explanation says exactly that about return value 0: "Note that this guarantees that the return value will be >= 0 if and only if the key is found. >

Re: About Question enthuware.ocpjp.v8.2.1307 :

Posted: Mon Jan 15, 2018 5:13 pm
by thodoris.bais
That's the thing, as Elecded explained.

It might be still somehow confusing in my mind, but I believe the reason behind 0 as the search index for "c" key is that our comparator is based on length and not on the natural (alphabetical) order of elements. "d" is of the same length and the key that we search, so, our custom Comparator class returns 0

Re: About Question enthuware.ocpjp.v8.2.1307 :

Posted: Mon Jan 15, 2018 10:28 pm
by admin
thodoris.bais wrote:That's the thing, as Elecded explained.

It might be still somehow confusing in my mind, but I believe the reason behind 0 as the search index for "c" key is that our comparator is based on length and not on the natural (alphabetical) order of elements. "d" is of the same length and the key that we search, so, our custom Comparator class returns 0
Your understanding is correct.

Re: About Question enthuware.ocpjp.v8.2.1307 :

Posted: Mon Apr 16, 2018 12:40 pm
by Bikfic
To my understanding

Code: Select all

Arrays.binarySearch
only works if the array has been previously sorted. ("The array must be sorted (as by the

Code: Select all

sort(byte[])
method) prior to making this call." --> Otherwise)
The array

Code: Select all

String[] strings = {"d", "bbb", "aaaa"};
is not sorted. Why is the result not "undefined" then?

Thank you!

Re: About Question enthuware.ocpjp.v8.2.1307 :

Posted: Mon Apr 16, 2018 8:25 pm
by admin
The array needs to be sorted as per the comparator you are using to search. In this case, we are using MyComparator, which compares the length of the string and according to this comparator, the array is indeed sorted.

Re: About Question enthuware.ocpjp.v8.2.1307 :

Posted: Tue Apr 17, 2018 1:56 am
by Bikfic
Thanks! :cheers: :cheers:

Re: About Question enthuware.ocpjp.v8.2.1307 :

Posted: Wed Sep 04, 2019 7:38 pm
by dongyingname
Arrays.binarySearch() method returns the index of the search key, if it is contained in the list.
Would explain why

Code: Select all

Arrays.binarySearch(sa, "c", new MyStringComparator()); will return 0
is correct.

The "key" in
returns the index of the search key,
is defined by the comparator. In our case, the "key" is actually meant to be .length(), which is equal to 1 as to "d".
If "d" is passed into the method, 0 is returned because the string of length that equals to 1 is contained in the list and is located at index 0. From the Comparator point of view, the array is actually like {1, 3, 4} just because or compare() method forces the it to compare elements of the { "d", "bbb", "aaaa" } array by length.
Trying passing "d" (length=1), "ddd" (length=3)and "dddd" (length=4)into the call would return 0, 1, 2 respectively, which agrees to the quote and my conclusion.