double[] d = new double[] {1, 2, 3, 4, 1, 45, 98};
Arrays.sort(d);
for (double e : d) {
System.out.print(e + "   ");
}
System.out.println();
int binarySearch = Arrays.binarySearch(d, 1);
System.out.println(binarySearch);
为什么binarySearch返回值为1呢?

解决方案 »

  1.   

    骚年 int存出不了 1.0 你换成double就好了
      

  2.   

    package http.hello;import java.util.Arrays;
    public class Demo5 { public static void main(String []args){
    double[] d = new double[] {1, 1, 1, 1, 1, 1, 1};
    Arrays.sort(d);
    for (double e : d) {
    System.out.print(e + "   ");
    }
    System.out.println();
    int binarySearch = Arrays.binarySearch(d, 1.0);
    System.out.println(binarySearch);
    }
    }
      

  3.   

     * Searches the specified array of doubles for the specified value using
         * the binary search algorithm.  The array must be sorted
         * (as by the {@link #sort(double[])} method) prior to making this call.
         * If it is not sorted, the results are undefined.  If the array contains
         * multiple elements with the specified value, there is no guarantee which
         * one will be found.  This method considers all NaN values to be
         * equivalent and equal.