Code: Select all
static String[] sa = { "d", "bbb", "aaaa" };
Moderator: admin
Code: Select all
static String[] sa = { "d", "bbb", "aaaa" };
Your understanding is correct.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
Code: Select all
Arrays.binarySearch
Code: Select all
sort(byte[])
Code: Select all
String[] strings = {"d", "bbb", "aaaa"};
Would explain whyArrays.binarySearch() method returns the index of the search key, if it is contained in the list.
Code: Select all
Arrays.binarySearch(sa, "c", new MyStringComparator()); will return 0
is defined by the comparator. In our case, the "key" is actually meant to be .length(), which is equal to 1 as to "d".returns the index of the search key,
Users browsing this forum: Bing [Bot] and 50 guests