Java Collection Tutorial - Java Arrays.binarySearch(float[] a, float key)








Syntax

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

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

Example

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

Binary search needs sorted arrays.

/*  w  w  w. j av a2s  .co m*/
import java.util.Arrays;

public class Main {

   public static void main(String[] args) {

    // initializing sorted float array
    float floatArr[] = {5.2f,5.3f,5.9f,6.3f};

    // value to search
    float searchVal = 6.3f;

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

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

The code above generates the following result.