Java Array class binarySearch(Object[] a, Object key) example
Array class binarySearch(Object[] a, Object key) method example
Array class binarySearch(Object[] a, Object key) method example. This example shows you how to use binarySearch(Object[] a, Object key) method.This method Searches the specified array for the specified object using the binary search algorithm.
Here is the code:-
/**
* @Program that Searches the specified array for the specified object using
* the binary search algorithm.
* binarySearch13Array.java
* Author:-RoseIndia Team
* Date:-15-May-2008
*/
import java.util.*;
public class binarySearch13Array {
public static void main(String[] args) {
//Creates an object array
Object ob[]={"Girish","komal","Prakash","Vikas","Girish"};
//searching the element
int i=Arrays.binarySearch(ob,"Girish");
System.out.print("The element " + ob[i] +" is found at index: " + i);
}
} |
Output of the program:-
| The element Girish is found at index: 0 |
|