Java Arrays.binarySearch(double[] a, int fromIndex, int toIndex, double key)

Syntax

Arrays.binarySearch(double[] a, int fromIndex, int toIndex, double key) has the following syntax.

public static int binarySearch(double[] a,  int fromIndex,  int toIndex,  double key)

Example

In the following code shows how to use Arrays.binarySearch(double[] a, int fromIndex, int toIndex, double key) method.

Binary search needs sorted arrays.


//w w  w  . j ava 2s  . c  o  m
import java.util.Arrays;

public class Main {

   public static void main(String[] args) {

    // initializing sorted double array
    double doubleArr[] = {5.4,5.5,9.2,9.3,35.4};

    // value to search
    double searchVal = 5.4;

    // entering the range of index
    int retVal = Arrays.binarySearch(doubleArr,1,3,searchVal);

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

The code above generates the following result.