我有這樣一個類:泛型和unchecked錯誤
class BSTNode<K extends Comparable, V> {
K key;
BSTNode(K key, V value) { ... }
}
然後我使用
node.key.compareTo(root.key) >= 0
凡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)
應該可以嗎?
你能告訴我們'node'和'root'的變量聲明嗎? – Matt