0
我試圖執行binarysearchtree(添加法)仿製藥,但它給了我這樣那樣的錯誤:操作數類型的錯誤
genericstree.java:36: error: bad operand types for binary operator '>'
if (value > iterator.m_Value)
^
first type: T
second type: T
where T is a type-variable:
T extends Comparable<T> declared in class BinarySearchTree
genericstree.java:42: error: bad operand types for binary operator '<'
if (value < prev.m_Value)
^
first type: T
second type: T
where T is a type-variable:
T extends Comparable<T> declared in class BinarySearchTree
2 errors
代碼波紋管:
class BinarySearchTree<T extends Comparable<T>>
{
class Node<T extends Comparable<T>>
{
Node<T> left;
Node<T> right;
T m_Value;
Node(T value)
{
left = null;
right = null;
m_Value = value;
}
}
Node<T> m_Root;
BinarySearchTree() { m_Root = null; }
void addNode(T value)
{
Node<T> tmp = new Node<T>(value);
if (m_Root == null)
{
tmp.m_Value = value;
m_Root = tmp;
return;
}
Node<T> iterator = m_Root;
Node<T> prev = m_Root;
while (iterator != null)
{
prev = iterator;
if (value > iterator.m_Value)
iterator = iterator.right;
else
iterator = iterator.left;
}
if (value < prev.m_Value)
prev.left = tmp;
else
prev.right = tmp;
}
}
我認爲問題之替換爲T對象可以通過任何類型,然後它不能比較它們,但我擴展類與可比較,但它沒有解決它。
是什麼導致了這個問題,我該如何解決它?
不知道更換
。感謝您解決問題。 – kvway