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

Syntax

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

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

Example

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

Binary search needs sorted arrays.


//from   w  w  w .  ja v a2s. c o  m
import java.util.Arrays;

public class Main {
 
   public static void main(String[] args) {

    // initializing sorted short array
    short shortArr[] = {1,2,15,52,110};

    // value to search
    short searchVal = 15;

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

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

The code above generates the following result.