binarySearch() 是 Java Collections 類的內置方法,它返回對象在排序列表中的位置。有兩種不同類型的 Java 集合 binarySearch() 方法,可以根據其參數進行區分。
這些是:
- Java Collections binarySearch(List<? extends Comparable<? super T>> list, T key)
- Java Collections binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
Java Collections binarySearch(List<? extends Comparable<? super T>> list, T key)
此方法用於使用二進製搜索算法搜索指定對象的提供列表。在進行方法調用之前,必須根據指定的自然數對列表進行升序排序。如果列表未排序,則結果未定義。
Java Collections binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
此方法用於使用二進製搜索算法搜索指定對象的提供列表。在進行方法調用之前,必須根據指定的比較器將列表按升序排序。
用法
以下是 binarySearch() 方法的聲明:
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
參數
參數 | 描述 | 必需/可選 |
---|---|---|
list | 由用戶指定要搜索。 | Required |
key | 它是要在提供的列表中搜索的鍵。 | Required |
c | 它是用於對列表進行排序的比較器。 | Required |
返回
方法 | 返回 |
---|---|
binarySearch(List<? extends Comparable<? super T>> list, T key) | 它在升序的排序列表中返回鍵的索引。 |
binarySearch(List<? extends T> list, T key, Comparator<? super T> c) | 它返回由比較器 c 定義的排序列表中的鍵的索引。 |
異常
ClassCastException - 如果列表的元素不能相互比較或者搜索關鍵字與列表的元素不能相互比較,則拋出異常。
兼容版本
Java 1.5 及以上
例子1
import java.util.*;
public class CollectionsBinarySearchExample1 {
public static void main(String[] args) {
//Create list
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("D");
//Search the list for key 'D'
int index = Collections.binarySearch(arrlist, "D");
System.out.println("index 'D' is available at position:"+index);
}
}
輸出:
index 'D' is available at position:3
例子2
import java.util.*;
public class CollectionsBinarySearchExample2 {
public static void main(String[] args) {
//Create list
ArrayList<Integer> arrlist = new ArrayList<Integer>();
arrlist.add(10);
arrlist.add(-20);
arrlist.add(30);
arrlist.add(-40);
arrlist.add(50);
//Print List
System.out.println("Provided List are:"+arrlist);
//Search the list for key '-20'
int index = Collections.binarySearch(arrlist, -20, Collections.reverseOrder());
System.out.println("Index '-20' is available at position:"+index);
}
}
輸出:
Provided List are:[10, -20, 30, -40, 50] Index '-20' is available at position:-4
例子3
import java.util.*;
public class CollectionsBinarySearchExample3 {
public static void main(String[] args) {
//Create list
ArrayList<Integer> arrlist = new ArrayList<Integer>();
arrlist.add(10);
arrlist.add(-20);
arrlist.add(30);
//Print List
System.out.println("Provided List are:"+arrlist);
System.out.print("Enter the search key:");
Scanner sc = new Scanner (System.in);
String key = sc.next();
int index = Collections.binarySearch(arrlist, key, Collections.reverseOrder());
System.out.println("Search Key is available at position:"+index);
sc.close();
}
}
輸出:
Provided List are:[10, -20, 30] Enter the search key:D Exception in thread "main" java.lang.ClassCastException:java.base/java.lang.Integer cannot be cast to java.base/java.lang.String at java.base/java.lang.String.compareTo(String.java:124) at java.base/java.util.Collections$ReverseComparator.compare(Collections.java:5140) at java.base/java.util.Collections$ReverseComparator.compare(Collections.java:5131) at java.base/java.util.Collections.indexedBinarySearch(Collections.java:333) at java.base/java.util.Collections.binarySearch(Collections.java:321) at myPackage.CollectionBinarySearchExample3.main(CollectionBinarySearchExample3.java:16)
示例 4
import java.util.*;
public class CollectionsBinarySearchExample4 {
public static void main(String[] args) {
//Create list
ArrayList<Integer> arlist = new ArrayList<Integer>();
arlist.add(5);
arlist.add(4);
arlist.add(2);
arlist.add(1);
arlist.add(3);
Collections.sort(arlist);
//Search for key
int index = Collections.binarySearch(arlist, 4, null );
System.out.println("4 is available at index:"+index);
}
}
輸出:
4 is available at index:3
例 5
import java.util.*;
public class CollectionsBinarySearchExample5 {
public static void main(String[] args) {
//Create a list
List<Domain> list = new ArrayList<Domain>();
list.add(new Domain(10, "www.JavaTpoint.com"));
list.add(new Domain(20, "www.sssit.com"));
list.add(new Domain(30, "www.hindi100.com"));
Comparator<Domain> c = new Comparator<Domain>()
{
public int compare(Domain u1, Domain u2)
{
return u1.getId().compareTo(u2.getId());
}
};
/* Searching a domain with key value 10. To search
we create an object of domain with key 10. */
int index = Collections.binarySearch(list, new Domain(10, null), c);
System.out.println("Available at index:" + index);
//Searching an item with key 5
index = Collections.binarySearch(list, new Domain(5, null), c);
System.out.println("Found at index:" +index);
}
}
//user-defined class to store domains with id and url
class Domain
{
private int id;
private String url;
// Constructor
public Domain(int id, String url)
{
this.id = id;
this.url = url;
}
public Integer getId()
{
return Integer.valueOf(id);
}
}
輸出:
Available at index:0 Found at index:-1
相關用法
- Java Collections binarySearch()用法及代碼示例
- Java Collections synchronizedSortedSet()用法及代碼示例
- Java Collections checkedQueue()用法及代碼示例
- Java Collections unmodifiableNavigableSet()用法及代碼示例
- Java Collections checkedSet()用法及代碼示例
- Java Collections copy()用法及代碼示例
- Java Collections checkedMap()用法及代碼示例
- Java Collections synchronizedNavigableSet()用法及代碼示例
- Java Collections singleton()用法及代碼示例
- Java Collections fill()用法及代碼示例
- Java Collections nCopies()用法及代碼示例
- Java Collections emptySet()用法及代碼示例
- Java Collections newSetFromMap()用法及代碼示例
- Java Collections checkedSortedMap()用法及代碼示例
- Java Collections addAll()用法及代碼示例
- Java Collections sort()用法及代碼示例
- Java Collections emptySortedSet()用法及代碼示例
- Java Collections max()用法及代碼示例
- Java Collections checkedSortedSet()用法及代碼示例
- Java Collections checkedCollection()用法及代碼示例
注:本文由純淨天空篩選整理自 Java Collections binarySearch() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。