加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Collections.binarySearch(列表,K键)澄清. Java的

发布时间:2020-12-14 23:47:04 所属栏目:Java 来源:网络整理
导读:鉴于以下声明,取自 this Oracle java教程,与类Collections的binarySearch()方法有关: The return value is the same for both forms. If the List contains the search key,its index is returned. If not,the return value is (-(insertion point) – 1) ,
鉴于以下声明,取自 this Oracle java教程,与类Collections的binarySearch()方法有关:

The return value is the same for both forms. If the List contains the
search key,its index is returned. If not,the return value is
(-(insertion point) – 1),where the insertion point is the point at
which the value would be inserted into the List,or the index of the
first element greater than the value or list.size() if all elements in
the List are less than the specified value.

为什么binarySearch()的返回值不仅返回负指数而不是负指数减1? (上面引用的粗体部分).

简而言之:为什么( – (插入点) – 1)而不仅仅是( – (插入点))?

提前致谢.

解决方法

那是因为 – (插入点)不明确.您将无法区分以下内容:

>在0位找到的项目;
>找不到项目,插入点为0.

使用 – (插入点) – 1时,上述两种情况会导致不同的返回值(0和-1).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读