集合类binarySearch()方法 (Collections Class binarySearch() method)

Syntax:

句法:

    public static int binarySearch(List l, Type key_ele);
public static int binarySearch(List l, Type key_ele, Comparator com);

  • binarySearch() method is available in java.util package.

    binarySearch()方法在java.util包中可用。

  • binarySearch(List l, Type key_ele) method is used to find the given object (key_ele) in the given list (l) with the help of binary search.

    binarySearch(List l,Type key_ele)方法用于在二进制搜索的帮助下在给定列表(l)中找到给定对象(key_ele)。

  • binarySearch(List l, Type key_ele, Comparator com) method is used to find the given object (key_ele) in the given list (l) and the list must be sorted based on defined Comparator object.

    binarySearch(List l,Type key_ele,Comparator com)方法用于在给定列表(l)中查找给定对象(key_ele),并且必须基于定义的Comparator对象对列表进行排序。

  • These methods may throw an exception at the time of finding the given element.

    这些方法在查找给定元素时可能会引发异常。

    ClassCastException: This exception may throw when the given parameter List elements that are mutually incomparable.

    ClassCastException :当给定的参数List元素彼此不可比较时,可能引发此异常。

  • These are static methods and it is accessible with the class name and if we try to access these methods with the class object then also we will not get any error.

    这些是静态方法,可以使用类名进行访问,如果尝试使用类对象访问这些方法,则也不会出现任何错误。

Parameter(s):

参数:

  • In the first case, "binarySearch(List l, Type key_ele)"

    在第一种情况下,“ binarySearch(List l,Type key_ele)”

    • List l – represents the list object.
    • 列表l –表示列表对象。
    • Type key_ele – represents the key element to be searched.
    • key_ele类型 –表示要搜索的关键元素。
  • In the second case, "binarySearch(List l, Type key_ele, Comparator com)"

    在第二种情况下,“ binarySearch(List l,Type key_ele,Comparator com)”

    • List l – represents the list object.
    • 列表l –表示列表对象。
    • Type key_ele – represents the key element to be searched.
    • key_ele类型 –表示要搜索的关键元素。
    • Comparator com – represents the Comparator object and we set the value to null that means it is natural or default order.
    • Comparator com –表示Comparator对象,我们将值设置为null表示它是自然顺序或默认顺序。

Return value:

返回值:

In both the cases, the return type of the method is int, it returns the position of the given key_ele(key element) when it exists in the given list.

在这两种情况下,该方法的返回类型均为int ,当它存在于给定列表中时,它将返回给定key_ele(key元素)的位置。

Example:

例:

// Java program to demonstrate the example
// of binarySearch() method of Collections
import java.util.*;
public class BinarySearchOfCollections {public static void main(String args[]) {// Instantiates an array list object
List < Integer > arr_l = new ArrayList < Integer > ();
// By using add() method is to add
// objects in an array list
arr_l.add(20);
arr_l.add(10);
arr_l.add(40);
arr_l.add(30);
arr_l.add(50);
// Display ArrayList
System.out.println("ArrayList: " + arr_l);
// By using binarySearch(arr_l,30,null) method is
// to search the the given object 30 in an arr_l
// based on defined comparator object (null) and
// here we are using null so list must be sorted
// in an ascending order
int indices = Collections.binarySearch(arr_l, 30, null);
// Display indices
System.out.println("Collections.binarySearch(arr_l,30,null): " + indices);
// By using binarySearch(arr_l,30) method is
// to search the the given object 30 in an arr_l
// so list must be sorted in an natural or ascending order
indices = Collections.binarySearch(arr_l, 30);
// Display indices
System.out.println("Collections.binarySearch(arr_l,30): " + indices);
}
}

Output

输出量

ArrayList: [20, 10, 40, 30, 50]
Collections.binarySearch(arr_l,30,null): -3
Collections.binarySearch(arr_l,30): -3

翻译自: https://www.includehelp.com/java/collections-binarysearch-method-with-example.aspx

Java Collections BinarySearch()方法与示例相关推荐

  1. java rotate,Java Collections rotate()方法与示例

    集合类rotate()方法rotation()方法在java.util包中可用. rotation()方法用于将List(l)元素旋转给定距离(dis). rotation()方法是静态方法,因此可以 ...

  2. Java Collections.emptyList() 方法的使用及注意事项

    Java Collections.emptyList方法的使用及注意事项 一.emptyList() 作用:返回一个空的List(使用前提是不会再对返回的list进行增加和删除操作): 好处: 1. ...

  3. Java Collections.frequency()方法具有什么功能呢?

    转自: Java Collections.frequency()方法具有什么功能呢? 下文笔者讲述Collections.frequency()方法的功能简介说明,如下所示: Collections. ...

  4. List元素互换,List元素转换下标,Java Collections.swap()方法实例解析

    Java Collections.swap()方法解析 jdk源码: public static void swap(List<?> list, int i, int j) {// ins ...

  5. Java IOUtils.copy方法代码示例(亲测)

    本文整理汇总了Java中org.apache.commons.io.IOUtils.copy方法的典型用法代码示例.如果您正苦于以下问题:Java IOUtils.copy方法的具体用法?Java I ...

  6. java reverselist_Java Collections reverse()方法与示例

    集合类reverse()方法reverse()方法在java.util包中可用. reverse()方法用于反转给定list(l)元素的顺序,换句话说,可以说此方法用于从右侧开始更改其元素的顺序. r ...

  7. java singletonlist_Java Collections singletonList()方法及示例

    集合类singletonList()方法 (Collections Class singletonList() method) singletonList() method is available ...

  8. java user directory,Java ProcessBuilder directory()方法与示例

    语法:public File directory (); public ProcessBuilder directory (File dir); ProcessBuilder类directory()方 ...

  9. Java序列化魔术方法及其示例使用

    在上一篇文章中, 您需要了解有关Java序列化的所有知识 ,我们讨论了如何通过实现Java序列化来启用类的可序列化性. Serializable接口. 如果我们的类未实现Serializable接口, ...

最新文章

  1. linux 卸载模块命令,Linux中module模块的编译、加载、卸载
  2. 【数据迁移】使用传输表空间迁移数据
  3. Java 集合List、Set、HashMap操作二(Map遍历、List反向、Set删除指定元素,集合只读、TreeMap操作、List转Array、List移动元素)
  4. 小程序和APP的差别是什么?
  5. 第5章 简易毛笔字(《Python趣味创意编程》教学视频)
  6. 智能判断图片中是否存在某物体_基于WT901传感器及NB-IOT无线技术开发的一款物体倾倒监测设备...
  7. Linux iptable文档
  8. xp计算机找不到音量调节,winxp系统电脑音量无法调节不能调节声音的恢复方案...
  9. 基于树莓派的语音对话助手 百度机器人 适合入门
  10. 使用Java制作一个魂斗罗游戏
  11. numpy一行转为一列
  12. 蓝牙扫描startLeScan测试
  13. Google Authenticator(谷歌身份验证器)C#版
  14. cad绘制正八边形_软件CAD | 各种“线”工具
  15. 架构师评价当前软件行业现状及发展前景
  16. 提高写作能力与表达能力
  17. leetcode-136-只出现一次的数字(java|python)
  18. php 动态网格,ZBrush中的动态网格该怎么进行运用
  19. 5G尚未完全覆盖,运营商开始采取措施降低5G投资
  20. 欧姆龙, PLC CJ2M标准程序,一共控制12个伺服电机 ,气缸若干,包含轴点动,回零

热门文章

  1. ygo游戏王卡组_ACG大科普(7)游戏王
  2. php system 255,GitHub - dwg255/OA-SYS: OA办公系统开源项目
  3. mysql5.6.msi 百度云_如何安装Mysql数据库类型msi(5.6)
  4. 1607: 字符棱形
  5. C# Collection was modified;enumeration operation may not execute
  6. Javascript基础之-原型(prototype)
  7. 《Java设计模式》之桥接模式
  8. SQLServer中的死锁的介绍
  9. 转:VMware安装Mac OS X Mavericks系统图文教程
  10. Flex Graphics