File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# 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.

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

Is this correct?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9374

19

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: 9374

19

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

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.

subject: K&B SCJP 5 Study Guide p628 problem 9