Java ClassCastException

Java ClassCastException

嗨,我是 Java 的新手,在构建 DoublyLinkedList 时遇到了这个问题。 运行 测试程序时出现此错误。任何人都可以建议对我的代码进行任何修复吗?任何帮助是极大的赞赏! 错误信息: 线程 "main" java.lang.ClassCastException 中的异常:hw3.Deque$Node 无法转换为 java.lang.Integer

  @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;
          }
      }
System.out.printf("\nThe winner is %d\n", dq.removeFirst());

removeFirst returns Node 不是 Integer.

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
      }
  }

这很可疑:@SuppressWarnings("unchecked")。除非你有充分的理由,否则不要禁止显示警告,在这些情况下,你通常应该添加说明此原因的注释。

如果删除此注释,您将在以下行收到编译警告:

return (E) head;

将其更改为 head.item 将解决此问题,但更有可能的是:

return temp.item;