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;

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

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