查找无序数组索引

int [] search = {17,5,82,85,5,1,25};
        for(int i = 0 ; i < search.length ; i++){
            if(search[i] == 5){
                System.out.println(i);
            }
        }
//结果 1
            4

 

//binarySearch 对已排序的数组进行数据的二分法查找
        int [] num7 = {12,34,26,75,45,62};
        Arrays.sort(num7);
        System.out.println(Arrays.toString(num7));   //进行升序[12, 26, 34, 45, 62, 75]
        System.out.println(Arrays.binarySearch(num7, 34));  //查找结果2:  地址 0 1 2 3 4 5
        System.out.println(Arrays.binarySearch(num7,77));     //查找结果-6: 地址-1 -2..-7

 

posted @ 2016-08-03 10:14  me丶不是好人  阅读(347)  评论(0编辑  收藏  举报