Java 泛型和未检查错误
我有一门课,像:Java 泛型和未检查错误,java,generics,Java,Generics,我有一门课,像: class BSTNode<K extends Comparable, V> { K key; BSTNode(K key, V value) { ... } } 其中node和root是BSTNode。在这一行中,我得到了一个未检查的错误。为什么? warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable
class BSTNode<K extends Comparable, V> {
K key;
BSTNode(K key, V value) { ... }
}
其中node
和root
是BSTNode
。在这一行中,我得到了一个未检查的错误。为什么?
warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable
} else if (node.key.compareTo(root.key) >= 0) { // new node >= root
^
where T is a type-variable:
T extends Object declared in interface Comparable
1 warning
根据我的理解,
BSTNode
中定义,K
应该扩展/实现Comparable
。因此node.key.compareTo(root.key)
应该可以吗?Comparable
也被泛化。请尝试以下操作:
class BSTNode<K extends Comparable<? super K>, V> { ... }
类BSTNodecompariable
也被泛化。请尝试以下操作:
class BSTNode<K extends Comparable<? super K>, V> { ... }
classbstnode该类应实现Comparable的通用版本。在您的情况下,可比
:
类节点{
K键;
BSTNode(K键,V值){}
}
该类应实现Comparable的通用版本。在您的情况下,可比
:
类节点{
K键;
BSTNode(K键,V值){}
}
您可以向我们展示节点
和根
的变量声明吗?您可以向我们展示节点
和根
的变量声明吗?为了获得最佳结果,请使用K扩展可比性为了获得最佳结果使用K扩展可比性为了获得最佳结果使用K延伸可比
// will cause the warning
BSTNode root = new BSTNode<Integer, Integer>(1, 1);
// will NOT cause the warning
BSTNode<Integer, Integer> root = new BSTNode<Integer, Integer>(1, 1);
class BSTNode<K extends Comparable<K>, V> {
K key;
BSTNode(K key, V value) {}
}