![](/img/trans.png)
[英]Making Collections.binarySearch() work with compareToIgnoreCase?
[英]How does Collections.binarySearch work?
我試圖了解Collections.binarySearch如何在Java中工作。 我不太了解我得到的輸出。
public static void main(String args[]) {
// create arraylist
ArrayList<String> arlst=new ArrayList<String> ();
arlst.add("A");
arlst.add("D");
arlst.add("C");
arlst.add("B");
arlst.add("E");
int index=Collections.binarySearch(arlst, "D", Collections.reverseOrder());
System.out.println(index);
}
}
此代碼的輸出為-1。
並且當按此順序插入元素時
arlst.add("D");
arlst.add("E");
arlst.add("C");
arlst.add("B");
arlst.add("A");
結果我得到0。 如果找不到元素,我認為負數是一個結果。 有人可以澄清我收到的輸出嗎?
您的數據必須根據給定的比較器進行排序,以便二進制搜索按預期工作。 (如果不是,則行為未定義。)
在進行此調用之前,必須根據指定的比較器(通過
sort(List, Comparator)
方法)將列表按升序sort(List, Comparator)
。
如果數據確實已排序,則該方法將返回所搜索元素的索引(如果已找到),否則(-(insertion point) - 1)
,如文檔中所指定。
例:
// Make sure it's sorted
Collections.sort(arlst, Collections.reverseOrder());
int index=Collections.binarySearch(arlst, "D", Collections.reverseOrder());
System.out.println(index); // prints 1
只是為了讓它更清楚 - 輸出為-1的原因 。 是的,你沒先排序是一個很大的錯誤。 但這里還有其他一些事情需要明確。
正如@aioobe在他的回答中所提到的那樣,但我認為他並沒有說清楚。 (-(insertion point) - 1)
是什么意思? 這是文檔所說的。
搜索鍵的索引(如果它包含在列表中); 否則, ( - (插入點) - 1) 。 插入點定義為鍵將插入列表的點: 第一個元素的索引大於鍵 ,或者list.size(),如果列表中的所有元素都小於指定的鍵。 請注意,當且僅當找到密鑰時,這可以保證返回值> = 0。
所以要使答案更清楚: -1 = -0 - 1
我想在此強調的是輸出可能是-2
或-3
或其他什么。 因為如果列表中的所有元素都小於指定的鍵 ,則輸出將為-list.size() - 1
。
■ Searches are performed using the binarySearch() method. ■ Successful searches return the int index of the element being searched. ■ Unsuccessful searches return an int index that represents the insertion point. The insertion point is the place in the collection/array where the element would be inserted to keep the collection/array properly sorted. * Return values and 0 indicate successful searches * Negative numbers to indicate insertion points * The first available insertion point is -1. Therefore,the actual insertion point is represented as (-(insertion point) -1)
以下是binarySearch方法的內部工作原理:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package binarysearch;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class BinarySearch {
static ArrayList<String> strings;
public static void main(String[] args) {
String sample = "a quick brown fox jumped right over the lazy dog while an active lion was nowhere in sight";
String[] simpleArray = sample.split(" ");
strings = new ArrayList(Arrays.asList(simpleArray));
Collections.sort(strings);
// Enter a search string; here it is "lazy"
binarySearch(strings, "lazy");
System.out.println("");
// Print the Array contents for convenience
printArrayList(strings);
}
static void printArrayList(ArrayList<String> strings) {
int i = 0;
for (String s: strings) {
i++;
System.out.println(i + s );
}
}
static void binarySearch (ArrayList<String> strings, String searchString) {
boolean debug = true;
int low = 0;
int high = strings.size();
int mid = 0;
while (low <= high) {
// The > symbol marks the hits before search is concluded
System.out.print(">");
mid = (high + low) / 2;
int comparison = strings.get(mid).compareToIgnoreCase(searchString);
if (comparison > 0) {
high = mid - 1;
} else if (comparison < 0) {
low = mid + 1;
} else {
int temp = mid;
System.out.println("Found '" + searchString + "' at: " + (temp + 1) );
break;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.