Java Array class binarySearch(int[] a, int key) method example code
Array class binarySearch(int[] a, int key) method example
Array class binarySearch(int[] a, int key) method example. This example shows you how to use binarySearch(int[] a, int key)method.This method Searches the specified array of ints for the specified value using the binary search algorithm.
Here is the code:-
/**
* @Program that Searches the specified array of ints for the specified value
* using the binary search algorithm.
* binarySearch9Array.java
* Author:-RoseIndia Team
* Date:-15-May-2008
*/
import java.util.*;
public class binarySearch9Array {
public static void main(String[] args) {
//Creating a Integer Array
int arr[]={1,5,7,4,7,6};
//Searching the array
int i=Arrays.binarySearch(arr,7);
//Displaying the element
System.out.println("The element "+arr[i]+" is found at index "+i);
}
} |
Output of the program:-
| The element 7 is found at index 2 |
|