当我尝试编写以下代码行时:
int foundIndex = Collections.<K>binarySearch(keys, key);
出现了错误:类型为Collections
的参数化方法<K>binarySearch(List<? extends Comparable<? super K>>, K)
不适用于参数(List<K>, K)
以上错误是什么意思,我的代码哪里出了问题?
// Comparator used to sort elements; may be null if elements are Comparable
public final Comparator<K> cmp;
{
super(new ArrayList<K>(), new ArrayList<V>());
cmp = new MyComparator<K>();
}
// Use the given comparator to sort the keys
//super(new ArrayList<K>(), new ArrayList<V>());
this.cmp = cmp;
}
{
if(!(key instanceof Comparable) && cmp == null)
throw new RuntimeException("The key is not instance of Comparable or comparator object is null");
}
public int indexOf(K key)
{
int foundIndex = Collections.<K>binarySearch(keys, key);
return foundIndex;
}
public int compareTo(K otherKey)
{
int result = 0;
for(int i = 0; i < keys.size(); i++)
{
result = ((Comparable<K>) keys.get(i)).compareTo(otherKey);
}
return result;
}
MyComparator类
import java.util.Comparator;
@Override
public int compare(K key1, K key2)
{
return -1;
}
}