Java Arrays 二分查找


Java Arrays 二分查找

package com.codingdict;

import java.util.Arrays;

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

      // initializing unsorted double array
      double doubleArr[] = {5.4,4.6,49.2,9.2,35.4};

      // sorting array
      Arrays.sort(doubleArr);

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

      // entering the value to be searched
      double searchVal = 5.4;

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

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