Java Arrays.binarySearch(char[] a, char key)

Syntax

Arrays.binarySearch(char[] a, char key) has the following syntax.

public static int binarySearch(char[] a,  char key)

Example

In the following code shows how to use Arrays.binarySearch(char[] a, char key) method.

Binary search needs sorted arrays.


/*from   ww  w  .ja v  a2s.co m*/

import java.util.Arrays;

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';

    int retVal = Arrays.binarySearch(charArr, searchVal);

    System.out.println("The index of e is : " + retVal);
  }
}

The code above generates the following result.