2013-04-07 71 views
-1

我正在使用單向鏈表來學習Java。單鏈表 - 無法對靜態類型進行靜態引用

我有一個工作鏈表,但它綁定到整數類型,我現在想通過改變所有的整數聲明泛型類型E.

編譯器不斷抱怨說,它「實現一個通用的類型化鏈表無法對非靜態類型E「進行靜態引用。代碼仍然運行。有誰知道如何解決這個錯誤「?

我認爲這是與一般的E型是靜態像整數或雙或如果它是像字符串或其他類的引用類型。

public class TestingLinkedList<E> { 


private E value; 
private TestingLinkedList<E> next; 

/* 
* Default Constructor 
* 
* @param value an absolute E value for the current Node 
* 
* @param next an absolute RecursiveLinkedList value for the current Node 
*/ 
public TestingLinkedList(E value, TestingLinkedList next) { 
    this.value = value; 
    this.next = next; 
} 

/* 
* Constructor Empty, when user supplies an empty for the constructor uses 
* value = - 1 and next = null as input parameters 
* 
* @param value an absolute int value for the current Node 
* @param next an absolute RecursiveLinkedList value for the current Node 
*/ 
public static final TestingLinkedList EMPTY = new TestingLinkedList(null,null) 
{ 
    public TestingLinkedList remove(E n) { 
     return this; 
    }; 

    public String toString() { 
     return ""; 
    }; 
}; 

/* 
* if the current node is null return false 
* else if current value is the chosen value 
* then return true. Otherwise call the contains 
* method of the next item in queue 
* 
* @param value an absolute int value for the current Node 
* @param RecursiveLinkedList object of the remove item 
* */ 
public TestingLinkedList remove(E n) { 
    if (value == n) { 
     return next; 
    } 
    // Call the remove method of the next Node if the selected Node is not 
    // the current node then construct and return the next method 
    return new TestingLinkedList(value, next.remove(n)); 
} 

/* 
* if the current node is null return false 
* else if current value is the chosen value 
* then return true. Otherwise call the contains 
* method of the next item in queue 
* 
* @param value an absolute int value for the current Node 
* @param boolean 
* */ 
public boolean contains(E n) { 
    if (next == null) { 
     return false; 
    }else if (value == n) { 
     return true; 
    } else { 
     return next.contains(n); 
    } 
} 

public String toString() { 
    return value + "," + next.toString(); 
} 


/* 
* 
* Testing Methods for RecursiveLinkedList 
* 
* */ 
public static void main(String[] args) { 
    TestingLinkedList l = new TestingLinkedList(1, 
      new TestingLinkedList(2, new TestingLinkedList(2, 
        new TestingLinkedList(3, new TestingLinkedList(4, 
          EMPTY))))); 
    System.out.println(" Test to String Method : " + l.toString()); 
    System.out.println(" Test remove method " + l.remove(1).toString()); 
    System.out.println(" Test contains method " 
      + String.valueOf(l.contains(4))); 
} 

}

+1

您的錯誤在哪裏? – Zyerah 2013-04-07 03:13:53

+0

塊if'(value == n){...}'也是可疑的。這應該是一個Object.equals()調用。 – 2013-04-07 03:58:35

回答

2
public static final TestingLinkedList EMPTY = new TestingLinkedList(null,null) 

應該是:

public static final TestingLinkedList<Integer> EMPTY 
             = new TestingLinkedList<Integer>(null,null) 

這違反了基因因爲E的佔位符是在施工時確定的,並且這是靜態的,所以它不能使用E值。請使用通用方法

public static <T> TestingLinkedList<T> empty() { 
    return new TestingLinkedList<T>(null, null) { 

     public TestingLinkedList remove(T n) { 
      return this; 
     } 

     public String toString() { 
      return ""; 
     } 
    } 
} 
+0

非常感謝您的幫助,但我對如何實施「空白」方式存在懷疑。我可能是錯的,但編譯器仍在抱怨。 – 2013-04-07 07:12:46

+0

哦,對不起。這應該有所幫助,請參閱編輯。 – Mordechai 2013-04-07 20:32:42

相關問題