aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused about Arrays.binarySearch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused about Arrays.binarySearch" Watch "Confused about Arrays.binarySearch" New topic
Author

Confused about Arrays.binarySearch

Harold Beemer
Greenhorn

Joined: Apr 16, 2006
Posts: 3
I don't understand how this method works. The output I get is:

Unsorted List
1 1 5 5 5 5 10 15
Search for 1:1
Search for 5:3
Search for 10:6


But shouldn't it be 0, 2, 6 as the first 1 is at index 0 and the first 5 is at index 2???


import java.util.*;

public ArrayOne() {
}

public static void main(String [] args){

Integer [] iol = new Integer[]{ new Integer(5),new Integer(5),new Integer(5),new Integer(5), new Integer(1), new Integer(1), new Integer(15), new Integer(10)};

Arrays.sort(iol);

System.out.println("Unsorted List");
String s = "";

for (int a : iol){
s = s+a + " ";
}
System.out.println(s);

System.out.println("Search for 1:" + Arrays.binarySearch(iol,1));
System.out.println("Search for 5:" + Arrays.binarySearch(iol,5));
System.out.println("Search for 10:" + Arrays.binarySearch(iol,10));

}

}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You should read the API for binarySearch:

"If the array contains multiple elements with the specified value, there is no guarantee which one will be found."


[My Blog]
All roads lead to JavaRanch
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

One more thing also there is not method named as BinarySearch which takes Integer Array as an parameter, instead of Integer Array it accepts int array.

Correct the program. and this is the reference of binary Search API

public static int binarySearch(int[] a,
int key)Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort method, above) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.


and the corrected program is:





Be Cool as
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Ankur,
try with java 1.5
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Satou kurinosuke:
Ankur,
try with java 1.5


I have tried dear, but still there is no method in Arrays Class called as BinarySearch which accepts Integer Array as an Input.

I think that you are asking about for Loop yes that is possible.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I have tried dear,

No you haven't, and maybe you should read something about autoboxing.

Try to compile with 1.5 and post your compiling error.
[ July 06, 2006: Message edited by: Satou kurinosuke ]
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Ok Fine,

I realized now. Actually I forgot this concept on my PC I have Jdk 1.4

installed and there it was giving compiling error. That's why I wrote this

statement.



Well Anyways I know autoboxing and autounboxing It's a New Implementations

in jAVA 1.5 in which If we need to pas anywhere int type variable and if we

are passing Integer Type object which will be automatically boxed in Inteer

Object.

Thankx for reminding me this feature.

Asha Pathik
Ranch Hand

Joined: Feb 08, 2006
Posts: 143
Hi Harold,

This link will help u understand how binary search internally works.

Asha


SCJP 1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused about Arrays.binarySearch