Arrays.binarySearch()是在Java中找到排序數組中元素的最簡單,最有效的方法
用法:
public static int binarySearch(data_type arr, data_type key )
其中data_type可以是任何原始數據類型:byte,char,double,int,float,short,long和Object。
描述:
使用二進製搜索算法在給定數據類型的指定數組中搜索指定值。在進行此調用之前,必須對數組進行排序(例如通過Arrays.sort()方法)。如果未排序,則結果不確定。如果數組包含具有指定值的多個元素,則不能保證將找到哪個元素。
參數:
arr-要搜索的數組
key–要搜索的值
返回:
搜索關鍵字的索引(如果包含在數組中);否則,(-(插入點)-1)。插入點定義為將鍵插入數組的點:第一個元素的索引大於鍵,如果數組中的所有元素都小於指定的鍵,則為a.length。請注意,這保證了當且僅當找到 key 時,返回值才會> = 0。
例子:
Searching for 35 in byteArr[] = {10,20,15,22,35} will give result as 4 as it is the index of 35 Searching for g in charArr[] = {'g','p','q','c','i'} will give result as 1 as it is the index of 'g' Searching for 22 in intArr[] = {10,20,15,22,35}; will give result as 3 as it is the index of 22 Searching for 1.5 in doubleArr[] = {10.2,15.1,2.2,3.5} will give result as -1 as it is the insertion point of 1.5 Searching for 35.0 in floatArr[] = {10.2f,15.1f,2.2f,3.5f} will give result as -5 as it is the insertion point of 35.0 Searching for 5 in shortArr[] = {10,20,15,22,35} will give result as -1 as it is the insertion point of 5
// Java program to demonstrate working of Arrays.
// binarySearch() in a sorted array.
import java.util.Arrays;
public class GFG
{
public static void main(String[] args)
{
byte byteArr[] = {10,20,15,22,35};
char charArr[] = {'g','p','q','c','i'};
int intArr[] = {10,20,15,22,35};
double doubleArr[] = {10.2,15.1,2.2,3.5};
float floatArr[] = {10.2f,15.1f,2.2f,3.5f};
short shortArr[] = {10,20,15,22,35};
Arrays.sort(byteArr);
Arrays.sort(charArr);
Arrays.sort(intArr);
Arrays.sort(doubleArr);
Arrays.sort(floatArr);
Arrays.sort(shortArr);
byte byteKey = 35;
char charKey = 'g';
int intKey = 22;
double doubleKey = 1.5;
float floatKey = 35;
short shortKey = 5;
System.out.println(byteKey + " found at index = "
+Arrays.binarySearch(byteArr,byteKey));
System.out.println(charKey + " found at index = "
+Arrays.binarySearch(charArr,charKey));
System.out.println(intKey + " found at index = "
+Arrays.binarySearch(intArr,intKey));
System.out.println(doubleKey + " found at index = "
+Arrays.binarySearch(doubleArr,doubleKey));
System.out.println(floatKey + " found at index = "
+Arrays.binarySearch(floatArr,floatKey));
System.out.println(shortKey + " found at index = "
+Arrays.binarySearch(shortArr,shortKey));
}
}
輸出:
35 found at index = 4 g found at index = 1 22 found at index = 3 1.5 found at index = -1 35.0 found at index = -5 5 found at index = -1
重要事項:
- 如果輸入列表未排序,則結果不確定。
- 如果有重複項,則不能保證將找到哪一個。
數組.binarysearch()與Java - Collections.binarySearch()用法及代碼示例
Arrays.binarysearch()適用於也可以是原始數據類型的數組。 Collections.binarysearch()適用於ArrayList和LinkedList之類的對象集合。
此方法有多種形式,其中我們還可以指定要搜索的數組範圍。我們將在後麵的文章中討論以及在Object數組中進行搜索。
相關用法
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java.util.function.BiPredicate用法及代碼示例
- Java Java.util.function.DoublePredicate用法及代碼示例
- Java Java.util.function.LongPredicate用法及代碼示例
- Java Java lang.Long.lowestOneBit()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java.util.concurrent.RecursiveTask用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
注:本文由純淨天空篩選整理自 Arrays.binarySearch() in Java with examples | Set 1。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。