package Sample;import java.util.*;public class H6 {
public static void main(String[] args) {
int[] numbers = new int[] { 2, 1, 3 };
Arrays.sort(numbers);
System.out.println(Arrays.binarySearch(numbers, 2));
}
}
binarySearch这个是干嘛的啊

解决方案 »

  1.   

    binarySearch
    public static int binarySearch(int[] a,
                                   int key)使用二分搜索法来搜索指定的 int 型数组,以获得指定的值。必须在进行此调用之前对数组进行排序(通过 sort(int[]) 方法)。如果没有对数组进行排序,则结果是不确定的。如果数组包含多个带有指定值的元素,则无法保证找到的是哪一个。 参数:
    a - 要搜索的数组
    key - 要搜索的值 
    返回:
    如果它包含在数组中,则返回搜索键的索引;否则返回 (-(插入点) - 1)。插入点 被定义为将键插入数组的那一点:即第一个大于此键的元素索引,如果数组中的所有元素都小于指定的键,则为 a.length。注意,这保证了当且仅当此键被找到时,返回的值将 >= 0。
      

  2.   

    http://my.donews.com/bigvan/2006/12/06/tdvnpcryidbnvtthqhblpdojzkuszlntoige/
      

  3.   

    API文档中有详细的说明,看哪个应该是最好的方法吧!