Comparable을 사용하여 연결 목록에서 일반 노드를 비교하는 방법?

연결된 목록을 사용하여 정렬 된 목록을 구현하고 있습니다. 내 노드 클래스는 다음과 같습니다

public class Node<E>{
    E elem;
    Node<E> next, previous;
}

정렬 된 목록 클래스에는 compareTo () 메서드 구현을 기반으로 제네릭 객체를 비교해야하는 add 메서드가 있지만이 구문 오류가 발생합니다. "E 유형에 대해 compareTo (E) 메서드가 정의되지 않았습니다." Node에서 compareTo 메서드를 구현하려고 시도했지만 E가 제네릭 유형이기 때문에 개체의 메서드를 호출 할 수 없습니다. 다음은 add (E elem) 메소드의 완성되지 않은 본문입니다.

public void add(E elem) 
{

        Node<E> temp = new Node<E>();
        temp.elem = elem;

        if( isEmpty() ) {           
            temp.next = head;
            head.previous = temp;
            head = temp;
            counter++; 
        }else{
            for(Node<E> cur = head; cur.next != null ; cur= cur.next) {
                **if(temp.elem.comparTo(cur.elem)) {**
                    //do the sort;

                }/*else{
                    cur.previous = temp;
                }*/             
            }
            //else insert at the end

        }
}

다음은 compareTo 메소드를 구현하는 객체 중 하나입니다

public class Patient implements Comparable<Patient>{
    public int compareTo(Patient that)
    {
        return (this.getPriority() <= that.getPriority() ? 1 : 0 );
    }
}
6
задан hash 15 June 2011 в 10:18
поделиться