How to use comparable to compare common nodes in a linked list?
                                        
                    •
                    Java                                    
                I'm using link lists to implement sort lists My node class looks like this
public class Node<E>{
    E elem;
    Node<E> next,prevIoUs;
}
I have an add method in the sort list class. I need to compare generic objects according to the implementation of the CompareTo () method, but I get this syntax error: "for type E, the method CompareTo (E) is not defined" I try to implement the CompareTo method in node, but I can't call the method of any object because e is a generic type This is the incomplete body of the add (E element) method
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
        }
}
This is an object that implements the CompareTo method
public class Patient implements Comparable<Patient>{
    public int compareTo(Patient that)
    {
        return (this.getPriority() <= that.getPriority() ? 1 : 0 );
    }
}
Solution
Bind e to comparable:
public class Node<E extends Comparable<E>>{
    E elem;
    Node<E> next,prevIoUs;
}
It will now compile
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        