Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.92 :

Posted: Mon Aug 14, 2017 11:34 am
by Aditya553
Still, i'm unable to understand why we get -5 to 3 .Pls, explain in a simpler manner.

Re: About Question com.enthuware.ets.scjp.v6.2.92 :

Posted: Mon Aug 14, 2017 9:16 pm
by admin
When you search for a string in a sorted list, there are two possibilities -
1. the string is found : In this case, it will return the index at which it was found.
2. the string is not found: In this case, it will return one more than the index at which this string will be inserted if you try to insert it in the list but with a minus sign.

In the given list, if you search for "xxxx", what will happen? It is not in the list, so the sign will be minus. And if you try to insert it, it will be inserted at index 5. So the return value will be -5.

Re: About Question com.enthuware.ets.scjp.v6.2.92 :

Posted: Mon Aug 14, 2017 9:37 pm
by Aditya553
i understood about -5 but why 3

Re: About Question com.enthuware.ets.scjp.v6.2.92 :

Posted: Mon Aug 14, 2017 10:25 pm
by admin
What all elements are there in the list? What will it return if you search for those elements? Apply the same logic given above.

Re: About Question com.enthuware.ets.scjp.v6.2.92 :

Posted: Wed Jan 19, 2022 10:21 am
by sijucm
I cannot think of any string that can come in between a and aa and aaa and aaaa. Could you give an example in which it can give -2, -4, -3, etc?
But if you do this, then you get -1. System.out.println(Arrays.binarySearch(sa, "1"));
According to what I understand, these are the values possible -5 (if higher), -1 (if less), 0, 1, 2, 3

Re: About Question com.enthuware.ets.scjp.v6.2.92 :

Posted: Wed Jan 19, 2022 11:30 am
by admin
Well, you can try searching for these strings - a1, aa1, and aaa1 and see what you get.