zoukankan      html  css  js  c++  java
  • java数组之binarySearch查找

    /**
     * 1.如果找到目标对象则返回<code>【公式:-插入点-1】</code>
     * 插入点:第一个大与查找对象的元素在数组中的位置,如果数组中的所有元素都小于要查找的对象,“插入点”就等于a.size()
    *@date:2018年6月22日
    *@author:zhangfs
    
    
    */
    public class ArraysBinarySearch {
    
        
        public static void main(String[] args) {
            int[] a= {9,8,7,1,2,6,4};
            Arrays.sort(a);
            System.out.println(Arrays.toString(a));
            
            int result=Arrays.binarySearch(a, 3);
            
            System.out.println("find value is:"+result);
        }
    }
    output:
    
    

    [1, 2, 4, 6, 7, 8, 9]

    
    

    find value is:-3

     

     注意事项:

    如果对未排序的数组进行binarySearch,结果将导致不准确,读者可以自行试验一下

  • 相关阅读:
    HDU1251 统计难题
    字典树模板
    HDU5536 Chip Factory(01字典树)
    函数的返回值
    函数的使用原则
    文件修改
    函数
    文件内指针移动
    文件操作模式
    字符编码
  • 原文地址:https://www.cnblogs.com/zhangfengshi/p/9215899.html
Copyright © 2011-2022 走看看