我正在使用BinaryNode实现二进制搜索树来存储数据。我在我的添加中使用CompareTo方法并包含确定项目所属的子树的方法。我在使用比较的各个地方不断收到此错误:
BST.java:50: error: cannot find symbol
if (item.CompareTo(root.data) > 0)
^
symbol: method CompareTo(T)
location: variable item of type T
where T is a type-variable:
T extends Comparable<? super T> declared in class BST
这是我的代码,我做错了什么?
import java.util.List;
import java.util.ArrayList;
import java.util.*;
import java.io.*;
public class BST<T extends Comparable<? super T>> implements BSTInterface<T>
{
private BinaryNode<T> root;
private int numberOfItems;
public List<T> preOrder = new ArrayList<T>();
public List<T> inOrder = new ArrayList<T>();
public List<T> postOrder = new ArrayList<T>();
public BST()
{
root = null;
numberOfItems = 0;
}
public BST(T rootData)
{
root = new BinaryNode<T>(rootData);
numberOfItems = 1;
}
public BST(T rootData, BST<T> leftTree, BST<T> rightTree)
{
root = new BinaryNode<T>(rootData);
numberOfItems = 1;
root.left = leftTree.root;
root.right = rightTree.root;
}
public void setTree(T rootData)
{
root = new BinaryNode<T>(rootData);
}
public boolean contains(T item)
{
if (root.data.equals(item))
return true;
else
{
if (item.CompareTo(root.data) > 0)
{
root = root.left;
return contains(item);
}
else if (item.CompareTo(root.data) < 0)
{
root = root.right;
return contains(item);
}
else
return false;
}
}
public void add(T newItem)
{
if (root == null)
{
root = new BinaryNode<T>(newItem);
numberOfItems++;
}
if (newItem.equals(root.data))
return;
if (newItem.CompareTo(root.data) < 0)
{
root = root.left;
add(newItem);
}
if (newItem.CompareTo(root.data) > 0)
{
root = root.right;
add(newItem);
}
}
答案 0 :(得分:2)
这不是c#:p
item.CompareTo()
change to:
item.compareTo()
compareTo
来自Comparable
界面。
答案 1 :(得分:0)
如果您要实施java.lang.Comparable
,则应该将该方法称为compareTo()
,而不是CompareTo()
。
为避免此类错误,建议您在方法前使用@Override
注释。这样Eclipse就会告诉您,您没有覆盖现有方法。
答案 2 :(得分:0)
[Comparable<T>][1]
有以下签名
int compareTo(T o)
Compares this object with the specified object for order.
但您正在使用CompareTo()
请更正案例,然后再尝试
答案 3 :(得分:0)
其compareTo()
而非CompareTo() // you are using capital C which is wrong
的 int java.lang.Comparable.compareTo(? super T o)
: - 强> 的
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception iff y.compareTo(x) throws an exception.)
The implementor must also ensure that the relation is transitive: (x.compareTo(y)>0 && y.compareTo(z)>0) implies x.compareTo(z)>0.
Finally, the implementor must ensure that x.compareTo(y)==0 implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all z.
It is strongly recommended, but not strictly required that (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
In the foregoing description, the notation sgn(expression) designates the mathematical signum function, which is defined to return one of -1, 0, or 1 according to whether the value of expression is negative, zero or positive.
Parameters:
o - the object to be compared.
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
Throws:
ClassCastException - if the specified object's type prevents it from being compared to this object.