0

私は次のようなクラスを持っています:

  class BSTNode<K extends Comparable, V> {
    K key;
    BSTNode(K key, V value) { ... }
  }

それから私は使用しています

node.key.compareTo(root.key) >= 0

noderootはどこですか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

私の理解では、 で定義されているようにBSTNodeKを拡張/実装する必要がありますComparable。それでnode.key.compareTo(root.key)OKのはず?

4

2 に答える 2

4

Comparableも一般化されています。次のことを試してください。

class BSTNode<K extends Comparable<? super K>, V> { ... }

また、宣言では必ず適切な型を使用してください。

// 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); 
于 2012-09-07T07:11:27.510 に答える
2

クラスは Comparable の汎用バージョンを実装する必要があります。あなたの場合Comparable<K>

class BSTNode<K extends Comparable<K>, V> {
   K key;
   BSTNode(K key, V value) {}
}
于 2012-09-07T07:13:54.750 に答える