有人可以解释为什么我收到此错误:方法AVLNode类型的undefinedTo(AVLNode)未定义
这是我的Tree类的简化版本:
public class AVLTree< E extends Comparable<E> >
{
private AVLNode<E> root ;
public AVLTree()
{
this.root = null;
}
public void insert ( AVLNode<E> item )
{
if( item.getLeft().compareTo(item.getItem() ) < 0) //ERROR IS HERE
{
}
}
}
以下是我的Node类的简短版本
class AVLNode <E extends Comparable<E> >
{
private AVLNode<E> item;
private AVLNode<E> left;
public AVLNode ( AVLNode<E> item)
{
this.item = item;
this.left = null;
}
public AVLNode( AVLNode<E> item, AVLNode<E> lft )
{
this.item = item;
this.left = lft;
}
public AVLNode<E> getItem()
{
return this.item;
}
public AVLNode<E> getLeft()
{
return this.left;
}
}
您的AVLNode
课程显然应该像这样:
public class AVLNode<E extends Comparable<E>> {
private E item;
//...
public int compareTo(final E obj) {
return this.item.compareTo(obj);
}
区别:
item
应该是类型的E
不是AVLNode<E>
因为要存储的é,而不是一个AVLNode
。为了说明您的AVLNode
s是可比较的,他们可以Comparable<T>
通过委派给E#compareTo()
method来实现自己:
public class AVLNode<E extends Comparable<E>> implements Comparable<AVLNode<E>> {
private E item;
//...
@Override
public int compareTo(final AVLNode<E> other) {
return this.item.compareTo(other.item);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句