【原】Arrays.binarySearch() 的用法
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() 的用法的更多相关文章
- Arrays.binarySearch采坑记录及用法
今天在生产环境联调的时候,发现一个很奇怪的问题,明明测试数据正确,结果却是结果不通过,经过debug查询到原来是Arrays.binarySearch用法错误,记录一下,避免后续再次犯错 具体测试如下 ...
- Arrays.binarySearch和Collections.binarySearch的详细用法
概述 binarysearch为在指定数组中查找指定值得索引值,该值在范围内找得到则返回该值的索引值,找不到则返回该值的插入位置,如果该值大于指定范围最大值则返回-(maxlength+1),而: i ...
- Java:集合,Arrays工具类用法
1. 描述 Arrays工具类提供了针对数组(Array)的一些操作,比如排序.搜索.将数组(Array)转换列表(List)等等,都为静态(static)方法: binarySearch - 使用二 ...
- Java的Arrays类 基本用法
初识Java的Arrays类 Arrays类包括很多用于操作数组的静态方法(例如排序和搜索),且静态方法可以通过类名Arrays直接调用.用之前需要导入Arrays类: import java.uti ...
- JAVA Arrays.binarySearch
转自:http://blog.csdn.net/somebodydie/article/details/8229343 package com.jaky; import java.util.*; pu ...
- Java中数组Arrays.binarySearch,快速查找数组内元素位置
在数组中查找一个元素,Arrays提供了一个方便查询的方法.Arrays.binarySearch(): 测试列子: public class MainTestArray { public stati ...
- Arrays.binarySearch 数组二分查找
public static void main(String[] args) throws Exception { /** * binarySearch(Object[], Object key) a ...
- 从数组中查看某值是否存在,Arrays.binarySearch
Arrays.binarySearch为二分法查询,注意:需要排序 使用示例 Arrays.binarySearch(selectedRows, i) >= 0
- java 用Arrays.binarySearch解读 快速定位数字范围
在一些时候,需要用给一个数字找到适合的区间,Arrays.binarySearch可达到这个目的. static int binarySearch(int[] a, int key) ...
随机推荐
- php class类的用法详细总结
以下是对php中class类的用法进行了详细的总结介绍,需要的朋友可以过来参考下 一:结构和调用(实例化): class className{} ,调用:$obj = new className(); ...
- PHP字符
匹配查找 strstr strpos 通常用在表单验证里面可以用到 substr 正值表达式匹配 preg_mathc(), preg)mathc_all() , preg_grep() 编码格式的转 ...
- SQL注入原理二
随着B/S模式应用开发的发展,使用这种模式编写应用程序的程序员也越来越多. 但是由于程序员的水平及经验也参差不齐,相当大一部分程序员在编写代码的时候 ,没有对用户输入数据的合法性进行判断,使应用程序存 ...
- Oracle RAC ORACLE_SID的设置,报错(ORA-01078, LRM-00109)
[oracle@zen2 ~]$ echo $ORACLE_SID ORAC [oracle@zen2 ~]$ sqlplus /nolog SQL :: Copyright (c) , , Orac ...
- SWFUpload下载地址
SWFUpload托管在谷歌代码上面,点击下载: https://code.google.com/p/swfupload/
- [转载]MongoDB学习(三):MongoDB Shell的使用
MongoDB shell MongoDB自带简洁但功能强大的JavaScript shell.JavaScript shell键入一个变量会将变量的值转换为字符串打印到控制台上. 下面介绍基本的操作 ...
- 常用的python模块
http://tiankonghaikuo1000.blog.163.com/blog/static/18231597200812424255338/ adodb:我们领导推荐的数据库连接组件bsdd ...
- HDU 2370 Convert Kilometers to Miles
点我看题目 题意 : 按照题目给定的规则将公里转化成英里,就是每个数都可以用斐波那契数列里的数表示,每个数都有一个编码,21可以表示成(1,0,0,0,0,0,0) ,13可以表示成(1,0,0,0, ...
- 过生日,也要学学哈,这次是SHELL的GETOPTS
今天是WEBSOCKET,, 先完成一个SHELL的GETOPS,周一就用得着. #!/bin/bash echo "usage: ./$0 -t (prism|opscripts)&quo ...
- 数据库备份,远程执行SHELL脚本
小小的东东,用于数据库的备份. 留存. #!/bin/sh keepDays= currentTime=`date "+%Y-%m-%d-%H-%M"` backPath=&quo ...