• 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

K&B SCJP 5 Study Guide p628 problem 9

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know how to search for a solution to this problem. I'm sure that this question has been asked before.

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

Answer:

int[] arr = new int[5];
arr[0] --> 0 or -1
arr[1] --> 1 or -2
arr[2] --> 2 or -3
arr[3] --> 3 or -4
arr[4] --> 4 or -5
after arr[4] --> -6
answer -6 through 4

Is this correct?
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

C:\Documents and Settings\Harry Henriques>java BinarySearch
int [] ia = new int []{ 2, 4, 6, 8, 10 };
insert 1 at index 0 is -1
ia[0] = 2 at index 0
insert 3 at index 1 is -2
ia[1] = 4 at index 1
insert 5 at index 2 is -3
ia[2] = 6 at index 2
insert 7 at index 3 is -4
ia[3] = 8 at index 3
insert 9 at index 4 is -5
ia[4] = 10 at index 4
insert 11 at index 5 is -6


 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this a question?? You didn't write anything apart from the code and the output...
 
Harry Henriques
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote: Is this a question?? You didn't write anything apart from the code and the output...



Ankit, I looked at the links that you provided, but the answers to my question weren't easy for me to understood. I thought that I could provide a better answer to my own question, and I tried to do so with this example code and output. Maybe this code snippet will help someone else.

Harry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic