Arrays.binarySearch() 的用法

1.binarySearch(Object[] a, Object key)

Searches the specified array for the specified object using the binary search algorithm.

参数1:a是要查询的数组;参数2:key是要查询的关键字;返回值是key所在数组的索引值,如果没有找到就返回-1

注意:该数组必须是升序排列的

2.查看具体源代码:

private static int binarySearch0(Object[] a, int fromIndex, int toIndex,

Object key) {

int low = fromIndex;

int high = toIndex - 1;

while (low <= high) {

int mid = (low + high) >>> 1;

Comparable midVal = (Comparable)a[mid];

int cmp = midVal.compareTo(key);

if (cmp < 0)

low = mid + 1;

else if (cmp > 0)

high = mid - 1;

else

return mid; // key found

}

return -(low + 1);  // key not found.

}

3.自己写例子使用

import java.util.Random;

public class Test {

public static void main(String[] args) {

Object[] a = new Object[1000];

for(int i=0;i<a.length;i++){

Integer myint = i+1;

a[i]=myint;

}

Integer key = new Random().nextInt(a.length);

int keyindex = mybinarySearch0(a,0,a.length,key);

if(keyindex>=0)

System.out.println("找到的key:"+a[keyindex]);

}

private static int mybinarySearch0(Object[] a, int fromIndex, int toIndex,

Object key) {

int low = fromIndex;

int high = toIndex - 1;

//这也是为什么数组必须升序排列的

while (low <= high) {

//>>> 无符号右移,高位补0,这里相当于/2

int mid = (low + high) >>> 1;

Comparable midVal = (Comparable) a[mid];

int cmp = midVal.compareTo(key);

System.out.println(midVal+".compareTo("+(key)+")");

if (cmp < 0)

low = mid + 1;

else if (cmp > 0)

high = mid - 1;

else

return mid; // key found

}

return -(low + 1); // key not found.

}

}

4.运行结果

【原】Arrays.binarySearch() 的用法的更多相关文章

  1. Arrays.binarySearch采坑记录及用法

    今天在生产环境联调的时候,发现一个很奇怪的问题,明明测试数据正确,结果却是结果不通过,经过debug查询到原来是Arrays.binarySearch用法错误,记录一下,避免后续再次犯错 具体测试如下 ...

  2. Arrays.binarySearch和Collections.binarySearch的详细用法

    概述 binarysearch为在指定数组中查找指定值得索引值,该值在范围内找得到则返回该值的索引值,找不到则返回该值的插入位置,如果该值大于指定范围最大值则返回-(maxlength+1),而: i ...

  3. Java:集合,Arrays工具类用法

    1. 描述 Arrays工具类提供了针对数组(Array)的一些操作,比如排序.搜索.将数组(Array)转换列表(List)等等,都为静态(static)方法: binarySearch - 使用二 ...

  4. Java的Arrays类 基本用法

    初识Java的Arrays类 Arrays类包括很多用于操作数组的静态方法(例如排序和搜索),且静态方法可以通过类名Arrays直接调用.用之前需要导入Arrays类: import java.uti ...

  5. JAVA Arrays.binarySearch

    转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...

  6. Java中数组Arrays.binarySearch,快速查找数组内元素位置

    在数组中查找一个元素,Arrays提供了一个方便查询的方法.Arrays.binarySearch(): 测试列子: public class MainTestArray { public stati ...

  7. Arrays.binarySearch 数组二分查找

    public static void main(String[] args) throws Exception { /** * binarySearch(Object[], Object key) a ...

  8. 从数组中查看某值是否存在,Arrays.binarySearch

    Arrays.binarySearch为二分法查询,注意:需要排序 使用示例 Arrays.binarySearch(selectedRows, i) >= 0

  9. java 用Arrays.binarySearch解读 快速定位数字范围

    在一些时候,需要用给一个数字找到适合的区间,Arrays.binarySearch可达到这个目的. static int binarySearch(int[] a, int key)          ...

随机推荐

  1. winform水晶报表编译错误,未能加载文件或程序集"file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framewor "

    未能加载文件或程序集“file:///C:\Program Files\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Commo ...

  2. 我们都忽略了Html5的力量,如果只看成一种技术就大错特错了!

    第一部分:Html5市场的力量 我们太忽略Html5的市场力量了.如果你把Html5当作一种技术,就大错特错了!忘掉你的产品,忘掉你的技术,想想移动时代的信息传播和消费场景.作为2B,我们实在是没有重 ...

  3. node.js里的forEach()也是异步的吗?

    博客已经迁移到www.imyzf.com,本站不再更新,请谅解! node里几乎所有用到回调函数的地方,都是异步的,回调函数后面的代码很可能比回调函数中的代码后先执行,特别是数据库操作.当然,node ...

  4. FireMonkey消息机制

    interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Classes, FMX.Forms, FMX.Plat ...

  5. HBASE的安装

    HBASE的安装: 安装的软件版本:hbase-0.98.4-hadoop2.tar.gz 下载链接:http://www.apache.org/dist/hbase/hbase-0.98.4/ 1. ...

  6. <一> SQL 基础

    删除数据库 drop database database-name 创建新表格 create table tablename (col1 type1 [not null] [primary key], ...

  7. centos 卸载vsftpd方法

    centos 卸载vsftpd方法 在服务器上安装了vsftpd,配置出错需要卸载vsftpd.卸载vsftpd的命令如下: 1 [root@localhost ~]# rpm -aq vsftpd2 ...

  8. c++ 联合体

    联合体分配的内存大小是成员变量中最大变量的大小 联合体的成员变量共享内存 小段模式(X86就是) 低位数据存在低地址单元 大端模式                     高位字节存在低地址单元

  9. 在安装MySQL Workbentch的时候出现如下问题,已经解决。

    mysql workbench cannot be executed from a path that contains non-ASCII characters. this problem is i ...

  10. Cassandra命令行CLI的基本使用

    启动cassandra-cli服务之后,可以进行CQL的使用. 1. 创建keyspace 可以理解成关系数据库的database [default@testkeyspace] create keys ...