2015-05-04 28 views
-1

您好,我對Java非常陌生,並且在構建DoublyLinkedList時遇到了這個問題。運行測試程序時出現此錯誤。任何人都可以建議我的代碼修復?任何幫助是極大的讚賞! 錯誤消息: 異常在線程 「主要」 java.lang.ClassCastException:hw3.Deque $節點不能轉換爲java.lang.Integer中Java ClassCastException

@SuppressWarnings("unchecked") 
public E removeFirst(){     // delete and return the item at the front 
     Node temp=head; 
     if (isEmpty()){ throw new NoSuchElementException(); } 
     else{ 
      if(head.next==null){ 
       head=null; 
       tail=null; 
      }else{ 
        head=head.next; 
        head.prev=null; 
       } 
      return (E) head; 
      } 
     } 
+1

請[閱讀](http://stackoverflow.com/help/mcve)瞭解如何創建**最小,完整且可驗證的**示例。 – Docteur

回答

0

這是非常可疑的:@SuppressWarnings("unchecked")。除非你有很好的理由,否則不要壓制警告,在這種情況下,你通常應該添加一條評論來說明這個原因。

如果去掉這個註釋,你會得到一個編譯以下行警告:

return (E) head; 

此更改爲head.item會解決這個問題,但它更可能是它應該是:

return temp.item; 
+0

嗨,對不起,我可以請你用另一個問題麻煩你? – Robert

+0

@Robert最好創建一個新問題 – wvdz

0
System.out.printf("\nThe winner is %d\n", dq.removeFirst()); 

removeFirst回報NodeInteger

public E removeFirst(){ 
    Node temp=head; 
    if (isEmpty()){ throw new NoSuchElementException(); } 
    else{ 
     if(head.next==null){ 
      head=null; 
      tail=null; 
     }else{ 
       head=head.next; 
       head.prev=null; 
      } 
     return (E) head; // head is of type Node 
     } 
    } 
+0

不,它返回E. – wvdz

+0

@popovitsj感謝您的支持。 – user2418306

+0

@ user2418306解決任何想法?謝謝 – Robert