• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Kathy Sierra doubt

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Q - This is a question from Kathy Sierra (for SJP 5)


Given a properly prepared String array containing five elements, which range of results could a
proper invocation of Arrays.binarySearch() produce?

A. 0 through 4
B. 0 through 5
C. -1 through 4
D. -1 through 5
E. -5 through 4
F. -5 through 5
G. -6 through 4
H. -6 through 5

Book says answer is -6 through 4.

I think the answer should be -5 through 4.

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Simran Dass wrote:
I think the answer should be -5 through 4.



Please do a search first... as this question comes up often.


But to answer your questions.... here is an excerpt from the JavaDoc:

JavaDoc wrote:Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). 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, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.




Basically, when a match occurs, you will get an index of the match. In this case, it is 5 items with an index of zero to four. If there isn't a match, then you get an insertion point. The insertion point is defined as the first index that is greater than the searched value. Also, if nothing is greater, then you will get a value that is the size of the array. In this case, the possible insertion points would be zero to five (one for each index, and the size if it is greater than everything).

Of course, it is not possible to determine whether the result is an index or insertion point, if it just returns it. So, it doesn't something silly. If it is an insertion point then it negates and subtracts one more. This causes the result to be negative and guaranteed to be not zero.

Anyway, take zero, negate and subtract one, and you get negative one. Take five, negate and subtract one, and you get negative six. This gives the possible range for an insertion point return to be negative one to negative six. And an overall response of negative six to four.

Henry
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


But , its given that the array has five elements ( i.e. the indexes are 0,1,2,3,4 )
So the last positon where you can insert is at index 4. So should not the insertion point
be -4-1 = -5 . So where does -6 come from ??
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


ok ok - I didn't read the size() thing carefully.
 
Simran Dass
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks a lot Henry. Great help !!
 
reply
    Bookmark Topic Watch Topic
  • New Topic