Mjall2
Mjall2

Reputation: 263

Getting a compilation error when implement interface comparable

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

Upvotes: 0

Views: 134

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198033

You're trying to compare the nodes, when it's only the values inside the nodes that are Comparable. Instead of temp.compareTo(temp.next), you probably want something like temp.value.compareTo(temp.next.value).

Upvotes: 2

Related Questions