[英]BinarySearch gives an erroe type “Collections is not applicable for the arguments”
[英]Java Collections Binary Search : The method binarySearch in the type Collections is not applicable for the arguments
我正在嘗試設計乘車共享系統。 這是我的基本對象
package rider;
import java.util.TreeMap;
public class Uber{
String driver;
TreeMap<Float,String> destination;
public Uber(String d)
{
driver=d;
destination = new TreeMap<Float,String>();
}
private void addTimeDest(float tm, String dest)
{
destination.put(tm, dest);
}
float getTsum() {
float tsum=0;
for (float f : this.destination.keySet())
tsum+=f;
return tsum;
}
}
因此,每個對象都有一個驅動程序和該驅動程序的關聯時間目標地圖。 最終,我想按時間字段(即樹形圖的關鍵字)對此類對象的列表進行排序。
這是我為上面創建的迭代器類
package rider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
public class UberIterator implements Iterator<Uber> {
int currIndex=0;
ArrayList<Uber> uList;
Comparator<Uber> timeComparator = new Comparator<Uber>(){
public int compare(Uber u1, Uber u2) {
return (int) (u1.getTsum()-u2.getTsum());
}
};
public UberIterator(ArrayList<Uber> nList)
{
uList=nList;
Collections.sort(uList,timeComparator);
}
public boolean hasNext() {
return currIndex<uList.size();
}
public Uber next() {
return uList.get(currIndex++);
}
@Override
public void remove() {
uList.remove(currIndex--);
}
public void remove(String d) {
int rindex=-1;
for(int u=0 ; u<currIndex; u++)
{
if(uList.get(u).driver.equals(d))
{
rindex=u;
break;
}
}
if(rindex<0)
System.out.println("Driver not found.");
else
{
uList.remove(rindex);
currIndex--;
}
}
public void remove(float tm) {
int rindex=Collections.binarySearch(uList, tm, timeComparator);
if(rindex<0)
{
System.out.println("Exact time not found. Closest will be removed.");
}
else
{
uList.remove(rindex);
currIndex--;
}
}
}
基本上,與比較器
Comparator<Uber> timeComparator = new Comparator<Uber>(){
public int compare(Uber u1, Uber u2) {
return (int) (u1.getTsum()-u2.getTsum());
}
};
我試圖按內部樹狀圖的鍵排序。 但我得到這個錯誤
The method binarySearch(List<? extends T>, T, Comparator<? super T>) in the type Collections is not applicable for the arguments (ArrayList<Uber>, float, Comparator<Uber>)
在
int rindex=Collections.binarySearch(uList, tm, timeComparator);
我應該如何糾正我的實現?
跟進
有沒有辦法重寫Collections.binarySearch
? 如果Uber
實現Comparable
並且在上面定義了compare方法,該怎么辦? 那不應該自動搜索time
維度嗎? 否則,定義用於排序的自定義比較器有什么好處? 我想要以某種方式對列表進行排序的唯一原因是以后可以高效地對其進行搜索。
package rider;
import java.util.TreeMap;
public class Uber implements Comparable<Uber> {
String driver;
TreeMap<Float,String> destination;
public Uber(String d)
{
driver=d;
destination = new TreeMap<Float,String>();
}
private void addTimeDest(float tm, String dest)
{
destination.put(tm, dest);
}
public int compareTo(Uber u) {
return (int) (this.getTsum()-u.getTsum());
}
float getTsum() {
float tsum=0;
for (float f : this.destination.keySet())
tsum+=f;
return tsum;
}
}
int rindex = Collections.binarySearch(uList,tm,timeComparator);
您不能在List<Uber>
搜索float
。
您的替代方案...坦白講,效果並不理想。 您可以創建一個包含tm
值的假Uber
,並將其傳遞給Collections.binarySearch
。 您可以使用類似Guava的庫,調用Lists.transform(ubers, getTmFunction)
創建一個視圖,並將其傳遞給Collections.binarySearch
。 您可以自己重新實現二進制搜索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.