2012-04-10 55 views
0

我试图使用compareTo方法通用节点类型E.获得当实现接口相当的编译错误

我已经结合E至可比

public class LinkedList<E extends Comparable<E>> { 

    // ---------------------------------------------------------- 
    // Implementing the doubly linked nodes (static nested class) 
    // ---------------------------------------------------------- 

    private static class Node<E extends Comparable<E>> { 

这种方法isSorted是内实现LinkedList类中,Node类位于Linkedlist类中。

我不断收到编译错误“的方法的compareTo(LinkedList.Node)是未定义的类型LinkedList.Node”

我相信,只有弹出当E是不延长媲美,我的情况下, 。 有什么帮助吗?

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();} 
     } 

我检查这个线程已经,How to compare generic nodes in a linked list using Comparable?

它并没有帮助。

预先感谢您

Mjall2

回答

2

你试图比较节点,当它是Comparable节点内唯一的值。而不是temp.compareTo(temp.next),你可能想要类似temp.value.compareTo(temp.next.value)

+0

我感觉有多厚...... – Mjall2 2012-04-10 00:48:56