I am trying to using the compareTo method for a generic Node type E.
I have already bound E to Comparable
public class LinkedList<E extends Comparable<E>> {
// ----------------------------------------------------------
// Implementing the doubly linked nodes (static nested class)
// ----------------------------------------------------------
private static class Node<E extends Comparable<E>> {
This method isSorted is implemented within the LinkedList class, the Node class is within the Linkedlist class.
I keep getting a compilation error “The method compareTo(LinkedList.Node) is undefined for the type LinkedList.Node”
I believe that only pops up when E is not extending Comparable, in my case it is.
Any help?
public boolean isSorted( ){
if(!isEmpty()){
Node<E> temp = head;
boolean local = true;
int x=0;
while (temp.next != null){
x=temp.compareTo(temp.next);
if(x<0){
temp = temp.next;}
else {return local;}
}
return local;}
else{ throw new IllegalArgumentException();}
}
I checked this thread already, How to compare generic nodes in a linked list using Comparable?
It didn’t help.
Thank you in advance
Mjall2
You’re trying to compare the nodes, when it’s only the values inside the nodes that are
Comparable. Instead oftemp.compareTo(temp.next), you probably want something liketemp.value.compareTo(temp.next.value).