Java Arrays.binarySearch(int[] a, int key)
Syntax
Arrays.binarySearch(int[] a, int key) has the following syntax.
public static int binarySearch(int[] a, int key)
Example
In the following code shows how to use Arrays.binarySearch(int[] a, int key) method.
Binary search needs sorted arrays.
/*from www . ja va2 s. c o m*/
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// initializing sorted int array
int intArr[] = {10,20,25,32,55};
// value to search
int searchVal = 32;
int retVal = Arrays.binarySearch(intArr,searchVal);
System.out.println("The index of element 32 is : " + retVal);
}
}
The code above generates the following result.
Home »
Java Tutorial »
java.util »
Java Tutorial »
java.util »