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

具体测试如下:

想通过判断J是否存在数组中,结果发现出现如下错误

 public static void main(String[] args) {
String[] test ={"X","J","7","5","4","11","W8","W7"};
System.out.println("没排序结果 = [" + Arrays.binarySearch(test,"J") + "]");
}

运行结果:

没有排序 = [-7]

进行排序后判断:

 public static void main(String[] args) {
String[] test ={"X","J","7","5","4","11","W8","W7"};
System.out.println("没排序结果 = [" + Arrays.binarySearch(test,"J") + "]");
Arrays.sort(test);
System.out.println("排序后结果 = [" + Arrays.binarySearch(test,"J") + "]");
}

运行结果:

没排序结果 = [-7]
排序后结果 = [4]

在网上查询下具体原因及查询官方解释如下:

/**
* Searches the specified array for the specified object using the binary
* search algorithm. The array must be sorted into ascending order
* according to the
* {@linkplain Comparable natural ordering}
* of its elements (as by the
* {@link #sort(Object[])} method) prior to making this call.
* If it is not sorted, the results are undefined.
* (If the array contains elements that are not mutually comparable (for
* example, strings and integers), it <i>cannot</i> be sorted according
* to the natural ordering of its elements, hence results are undefined.)
* If the array contains multiple
* elements equal to the specified object, there is no guarantee which
* one will be found.
*
* @param a the array to be searched
* @param key the value to be searched for
* @return index of the search key, if it is contained in the array;
* otherwise, <tt>(-(<i>insertion point</i>) - 1)</tt>. The
* <i>insertion point</i> is defined as the point at which the
* key would be inserted into the array: the index of the first
* element greater than the key, or <tt>a.length</tt> if all
* elements in the array are less than the specified key. Note
* that this guarantees that the return value will be >= 0 if
* and only if the key is found.
* @throws ClassCastException if the search key is not comparable to the
* elements of the array.
*/
public static int binarySearch(Object[] a, Object key) {
return binarySearch0(a, 0, a.length, key);
}

使用二分搜索法来搜索指定数组,以获得指定对象。在进行此调用之前,必须根据元素的自然顺序对数组进行升序排序(通过 sort(Object[]) 方法)。如果没有对数组进行排序,则结果是不确定的。(如果数组包含不可相互比较的元素(例如,字符串和整数),则无法 根据其元素的自然顺序对数组进行排序,因此结果是不确定的。)如果数组包含多个等于指定对象的元素,则无法保证找到的是哪一个,故所以会出现此问题

如果要判断数组中是否存在,可以使用 ArrayUtils.contains这个方法来判断是否存在,

/**
* 校验服务类型是否符合枚举值
* @param possibility 可能性
* @param field 字段
* @param info 校验不通过的错误消息
*
*/
public void validatePossibility(String[] possibility,String field,ErrorInfo info){
boolean flag = ArrayUtils.contains(possibility,field);
if (!flag) {
throw getException(info) ;
}
}

Arrays.binarySearch采坑记录及用法的更多相关文章

  1. Charles 抓包工具安装和采坑记录

    Charles 抓包工具安装和采坑记录 网络抓包是解决网络问题的第一步,也是网络分析的基础.网络出现问题,第一步肯定是通过抓包工具进行路径分析,看哪一步出现异常.做网络爬虫,第一步就是通过抓包工具对目 ...

  2. Antd前端开发采坑记录

    背景 基于页面友好,界面整洁美观:基于Antd框架开发虾能平台 选型 基于Antd-admin工程架构,进行开发:基于Antd+React+Umj 采坑记录 按照Html方式天机onClick方法,每 ...

  3. HUE Oozie : error=2, No such file or directory采坑记录

    HUE Oozie : error=2, No such file or directory采坑记录 1.错误详情 一直都是同一种方式在hue上定义workflow,不知为啥 今天定义的就是不行... ...

  4. uni-app采坑记录

    1. uni-app采坑记录 1.1. 前言 这里记录下uni-app实践中踩的坑 1.2. 坑点 1.2.1. 触发事件@longTap和@longpress 这两个都表示长按触发事件,那么这两个有 ...

  5. angular采坑记录

    在angular中会遇到一些莫名的问题,导致不能完成想要的功能,可能是某项用法使用错误,或许是angular相对应不支持,或者是我们功力根本就没有达到.为了在每次采坑之后能有所收获,再遇到时能理解其根 ...

  6. v8环境搭建采坑记录

    项目组有把js接入C++服务求的需求,故开始了v8接入的工作,用了一天多时间,v8才在centos环境上成功安装,过程中踩了很多坑,下面将采坑过程记录如下: centos下编译安装v8:   查看ce ...

  7. Win7 node多版本管理gnvm采坑记录

    采坑描述:下载新node版本及切换node失败 解决:1.要用管理员权限启动cmd:2.确保node是空闲的 Gnvm下载地址: 32-bit | 64-bit Github 1.下载之后为 得到一个 ...

  8. Android Studio采坑记录

    折腾了几个月的Android Studio,终于在今天被我搞定了 ( ̄▽ ̄)~* 开贴记录下,免得下次再次采坑 先说下我之前电脑的环境配置吧,sdk是几年前在网上下载别人整理出来的包,一直没有更新过 ...

  9. golang采坑记录

    安装golang,引入第三方库,采坑 1.获取安装包 go语言中文网:https://studygolang.com/dl 官网地址:https://studygolang.com/dl 2.下载 选 ...

随机推荐

  1. 解决Maven 编译出的jar中没有主清单属性

    出现这个问题的原因是 pom 中没有添加主程序入口 在配置中添加如下配置 <plugin> <groupId>org.apache.maven.plugins</grou ...

  2. log4net SmtpAppender 踩坑总结

    错误集合: System.Net.Mail.SmtpException: 命令顺序不正确. 服务器响应为:Error: need EHLO and AUTH first ! System.Net.Ma ...

  3. FreeRTOS 任务创建和删除(静态)

    #define configSUPPORT_STATIC_ALLOCATION 1 //打开静态方法 StackType_t TaskStackBuffer[50]; //任务堆栈大小 StaticT ...

  4. mysql数据库事件

    今天在测试一个存储过程和数据库事件,就是到某一个固定时间,数据库自动调用一个存储过程实现一些功能. 单独来看事件是没有问题的 MINUTE STARTS '2015-12-09 02:00:00' O ...

  5. WCF Restful Service

    对 Web Services.WCF 和 Restful 的扫盲可参见:https://www.cnblogs.com/scy251147/p/3382436.html 关于之前对 WCF 的学习,可 ...

  6. mamcached+(magent+keepalived高可用)搭建及理论概述

    目录 一.理论概述 工作流程 二.部署 环境 环境概述 部署 三.测试 四.总结 一.理论概述 Memcached服务器端与PHP-Memcache客户端安装配置_服务器应用_Linux公社-Linu ...

  7. Django组件之用户认证

    auth模块 1 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1.1 .authenticate( ...

  8. STM32 HAL库学习系列第8篇---回调函数总结

    普通函数与回调函数的区别:就是ST将中断封装,给使用者的API,就是标准库的中断函数 对普通函数的调用: 调用程序发出对普通函数的调用后,程序执行立即转向被调用函数执行,直到被调用函数执行完毕后,再返 ...

  9. 链表(python)

    链表1.为什么需要链表顺序表的构建需要预先知道数据大小来申请连续的存储空间,而在进行扩充时又需要进行数据的搬迁,所以使用起来并不是很灵活.链表结构可以充分利用计算机内存空间,实现灵活的内存动态管理. ...

  10. java TCP 通信:服务端与客服端

    1.首先先来看下基于TCP协议Socket服务端和客户端的通信模型: Socket通信步骤:(简单分为4步) 1.建立服务端ServerSocket和客户端Socket 2.打开连接到Socket的输 ...