当尝试比较两个T值时,Intellij想法在类internalNode中给出了这个错误:compare(capture,capture)不能应用于(T,T)。我不明白为什么。我已经将参数化类型更改为T,它仍然显示消息但不是捕获只是T. 非常感谢任何帮助
public class BSpecialTree<T> {
private Node<T> root;
private int keysNumber;
private Comparator<? super T> cmp;
public BSpecialTree(Comparator<? super T> cmp, int keysNumber) {
this.cmp = cmp;
this.keysNumber = keysNumber;
}
public boolean belongs(T key) {
if(key == null) {
if(root == null)
return true;
return false;
}
return root.belongs(key);
}
private interface Node<T> {
boolean belongs(T key);
}
private class internalNode<T> implements Node<T> {
private T key;
private Node<T> left, right;
public internalNode(T key) {
this.key = key;
}
public boolean belongs(T key) {
if(cmp.compare(this.key, key) > 0) //ERROR
return left.belongs(key);
return right.belongs(key);
}
}
private class leafNode<T> implements Node<T> {
private List<T> keys;
public leafNode() {
keys = new ArrayList<T>(keysNumber);
}
public boolean belongs(T key) {
return keys.indexOf(key) != -1;
}
}
答案 0 :(得分:3)
您的内部类正在引入他们自己的通用类型<T>
,它隐藏了来自<T>
的{{1}},但比较器被声明为使用来自BSpecialTree<T>
的{{1}},不是来自T
。
也许尝试用
BSpecialTree