Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 泛型和未检查错误_Java_Generics - Fatal编程技术网

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> { ... }

类BSTNode
compariable
也被泛化。请尝试以下操作:

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) {}
}