日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java Collections BinarySearch()方法与示例

發布時間:2025/3/11 java 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Collections BinarySearch()方法与示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

集合類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 Collectionsimport java.util.*;public class BinarySearchOfCollections {public static void main(String args[]) {// Instantiates an array list objectList < Integer > arr_l = new ArrayList < Integer > ();// By using add() method is to add// objects in an array listarr_l.add(20);arr_l.add(10);arr_l.add(40);arr_l.add(30);arr_l.add(50);// Display ArrayListSystem.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 orderint indices = Collections.binarySearch(arr_l, 30, null);// Display indicesSystem.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 orderindices = Collections.binarySearch(arr_l, 30);// Display indicesSystem.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()方法与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。