>Intellij的想法在尝试比较两个T值时在类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;
}
}
您的内部类正在引入自己的泛型类型<T>
,该泛型类型对BSpecialTree<T>
隐藏<T>
,但比较器被声明为使用来自BSpecialTree
的T
,而不是来自internalNode
。
也许尝试
private class internalNode implements Node<T> {
// ^remove `<T>` from here