我怎样才能修复我把它们放在评论中的错误?

时间:2012-04-17 17:38:41

标签: java inheritance listiterator

我不明白为什么我在使用listIterator时遇到错误? 我收到错误的 spot 后面是评论。

这是我的代码:

   /**
      Returns an iterator for iterating through this list.
      @return an iterator for iterating through this list
  */
   public ListIterator<String> listIterator()
   {  
      return new LinkedListIterator();
      // error here it says: Type safety: The expression of type 
      // LinkedList.LinkedListIterator needs unchecked conversion to conform
      // to ListIterator<String>
   }

   class Node
   {  
      public Object data;
      public Node next;
   }

   class LinkedListIterator implements ListIterator 
   /* here also it says:The type LinkedList.LinkedListIterator must implement
     the inherited abstract method ListIterator.previousIndex() when i implement 
     the listIterator it says:ListIterator is a raw type. References to generic
      type ListIterator<E> should be parameterized*/

我不知道如何解决这些问题,所以我们会提供任何帮助。谢谢

2 个答案:

答案 0 :(得分:3)

在你的第一个方法

public ListIterator<String> listIterator()
   {  
      return new LinkedListIterator(); // error here it says: Type safety: The expression of type LinkedList.LinkedListIterator needs unchecked conversion to conform to ListIterator<String>
   }

您的返回类型是通用ListIterator,但您返回Non Generic LinkedListIterator。 在响应中,您只是警告它不是错误,您可以通过添加..

简单地删除它
@SuppressWarnings("unchecked")
public ListIterator<String> listIterator()
   {  
      return new LinkedListIterator(); // error here it says: Type safety: The expression of type LinkedList.LinkedListIterator needs unchecked conversion to conform to ListIterator<String>
   }

其次,当您添加未实现的方法时,您将收到另一个不是错误的警告。

class LinkedListIterator implements ListIterator

ListIterator不是通用的,您可以将其设为generic.i.e ListIterator<String>

你也可以通过告知你知道的编译器

让它保持原样来删除这个警告
 @SuppressWarnings("rawtypes")
class LinkedListIterator implements ListIterator 

答案 1 :(得分:1)

您收到此错误

LinkedList.LinkedListIterator must implement the inherited abstract method ListIterator.previousIndex()

因为LinkedListIterator需要实现ListIterator类中定义的所有方法。在您执行此操作之前,您的LinkedListIterator类型实际上并不存在。