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呢?
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呢?
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);
}
}
* 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.