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. javascript变量

    5种简单数据类型(基本数据类型) undefined  null  boolean  number  string (还有一种复杂的数据类型:object) 变量的两种不同的数据类型:基本类型(简单数 ...

  2. Spring 官方下载地址(非Maven)

    现在spring的官网停止了使用zip包下载,只能使用maven,非常的不方便,分享如下网址可以使用zip包下载,是不是方便多了!~ 下载列表如下: spring-framework-3.2.8.RE ...

  3. PHP的PSR-0命名标准

    PSR是Proposing a Standards Recommendation(提出标准建议)的缩写,是由PHP Framework Interoperability Group(PHP通用性框架小 ...

  4. mysql 查看数据库大小

    select table_schema, concat(truncate(sum(data_length)/1024/1024,2),' mb') as data_size,concat(trunca ...

  5. 9.MVC框架开发(关于ActionResult的处理)

    1.在Action处理之后,必须有一个返回值,这个返回值必须继承自ActionResult的对象 2.ActionResult,实际就是服务器端响应给客户端的结果 ViewResult(返回视图结果) ...

  6. android:Faild to install,你的主机中的软件终止了一个连接错误解决

    当在用真机调试android程序时出现Faild to install,你的主机中的软件终止了一个连接错误时可以这样解决: 在手机开启usb调试和安装未知来源软件的情况下: 1:先查进入任务管理器查看 ...

  7. MongoDB应用详解

    mongodb是一个用来存储管理数据的软件 他是一个 c/s 架构的软件,是一个网络类型的软件如果要是使用mongodb的话,首先需要开启mongodb的服务端,然后通过客户端软件去连接服务器 1.要 ...

  8. ASP.NET 学习博客

    ASP.NET MVC5 网站开发实践 http://www.cnblogs.com/mzwhj/p/3537145.html 基于MVC4+EasyUI的Web开发框架形成之旅 http://www ...

  9. Nhibernate 一对多,多对一配置

    先来分析下问题,这里有两张表:Users(用户表) U和PersonalDynamic(用户动态表) PD,其中PD表的UserId对应U表的Id 如图: 现在映射这两张表: 如图: User.hbm ...

  10. 388A Fox and Box Accumulation

    一开始贪心策略想错了! #include<cstdio> #include<algorithm> using namespace std; ]; int main() { in ...