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