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








Syntax

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

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

Example

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

Binary search needs sorted arrays.

/*from   ww w .  j av a  2 s . c  o 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;

    // entering range of index
    int retVal = Arrays.binarySearch(floatArr,1,4,searchVal);

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

The code above generates the following result.