比较器不让我比较 T 值
Comparator not letting me compare T values
Intellij idea 在尝试比较两个 T 值时在 class internalNode 中给我这个错误:compare(capture,capture) cannot be applied to (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>
,它隐藏了 <T>
来自 BSpecialTree<T>
,但是比较器被声明为使用来自 T
BSpecialTree
,不是来自 internalNode
。
也许试试
private class internalNode implements Node<T> {
// ^remove `<T>` from here
Intellij idea 在尝试比较两个 T 值时在 class internalNode 中给我这个错误:compare(capture,capture) cannot be applied to (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>
,它隐藏了 <T>
来自 BSpecialTree<T>
,但是比较器被声明为使用来自 T
BSpecialTree
,不是来自 internalNode
。
也许试试
private class internalNode implements Node<T> {
// ^remove `<T>` from here