File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B SCJP 5 Study Guide p628 problem 9 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B SCJP 5 Study Guide p628 problem 9" Watch "K&B SCJP 5 Study Guide p628 problem 9" New topic
Author

K&B SCJP 5 Study Guide p628 problem 9

Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
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?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Here's a bit of searching I did using the search query "K B binarySearch"

http://www.coderanch.com/t/436980/Programmer-Certification-SCJP/certification/binarysearch-method
http://www.coderanch.com/t/270611/Programmer-Certification-SCJP/certification/Chap-pg-from-kathy-Sierra
http://www.coderanch.com/t/266974/Programmer-Certification-SCJP/certification/Arrays-binarySearch-query

HTH


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206

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

Joined: Aug 03, 2008
Posts: 9291
    
  17

Is this a question?? You didn't write anything apart from the code and the output...
Harry Henriques
Ranch Hand

Joined: Jun 17, 2009
Posts: 206
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B SCJP 5 Study Guide p628 problem 9
 
Similar Threads
Array with enhanced for loop for clarification
Garbage Collection
here is a question and i have a very similar code but i need to convert it
Arrays
how to use array in array?