-3

我不明白为什么我在使用 listIterator 时会出错?我得到错误的地方是评论。

这是我的代码:

   /**
      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*/

我不知道如何解决它们,因此将采取任何帮助。谢谢你

4

2 回答 2

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,但您返回的是非通用 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 不是通用的,您可以将其设为通用。即ListIterator<String>

并且您还可以通过通知编译器您知道让它保持原样来删除此警告

 @SuppressWarnings("rawtypes")
class LinkedListIterator implements ListIterator 
于 2012-04-17T17:47:23.130 回答
1

You are getting this error

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

because LinkedListIterator needs to implement ALL of the methods defined in the ListIterator class. Until you do that, your LinkedListIterator type doesn't actually exist.

于 2012-04-17T17:42:51.610 回答