Java Arrays.binarySearch(char[] a, int fromIndex, int toIndex, char key)
Syntax
Arrays.binarySearch(char[] a, int fromIndex, int toIndex, char key) has the following syntax.
public static int binarySearch(char[] a, int fromIndex, int toIndex, char key)
Example
In the following code shows how to use Arrays.binarySearch(char[] a, int fromIndex, int toIndex, char key) method.
Binary search needs sorted arrays.
import java.util.Arrays;
//from w w w .j a va2s .co m
public class Main {
public static void main(String[] args) {
// initializing sorted char array
char charArr[] = {'a', 'c', 'd', 'e','f'};
// value to search
char searchVal = 'e';
// entering the range of index
int retVal = Arrays.binarySearch(charArr,1,5,searchVal);
System.out.println("The index of e is : " + retVal);
}
}
The code above generates the following result.
Home »
Java Tutorial »
java.util »
Java Tutorial »
java.util »