Comparable中的CompareTo无法应用于类

时间:2016-04-17 21:24:47

标签: java generics comparable

我收到一条错误,指出Comparable中的CompareTo可以应用于我的班级。

在我的第一堂课中,我有以下方法:

Public <AnyType extends Comparable<? super AnyType>> void add(AnyType element){
    ListNode itr = header;

    while(itr.next != null && element.compareTo(itr.next) > 0){

    }
}

错误发生在itr.next。

ListNode类:

class ListNode <AnyType extends Comparable<? super AnyType>>
{
      // Constructors
    public ListNode( AnyType theElement )
    {
        this( theElement, null );
    }

    public ListNode( AnyType theElement, ListNode n )
    {
        element = theElement;
        next    = n;
    }

    public AnyType   element;
    public ListNode next;
}

1 个答案:

答案 0 :(得分:0)

看起来你想要

element.compareTo(itr.next.element)

element.compareTo(itr.next)
相关问题