Java Arrays 二分查找 Float 索引


Java Arrays 二分查找 Float 索引

package com.codingdict;

import java.util.Arrays;

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

      // initializing unsorted float array
      float floatArr[] = {5.2f,46.1f,42.9f,22.3f};

      // sorting array
      Arrays.sort(floatArr);

      // let us print all the elements available in list
      System.out.println("The sorted float array is:");
      for (float number : floatArr) {
         System.out.println("Number = " + number);
      }

      // entering the value to be searched
      float searchVal = 22.3f;

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

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