【问题标题】:Iterator generics, cannot declare an iterator of mismatching types (maybe casting issue)迭代器泛型,不能声明类型不匹配的迭代器(可能是转换问题)
【发布时间】:2012-04-11 00:37:44
【问题描述】:

我有这个简单的代码

public String toString() {

   **Iterator it = list.iterator();**
   String s;

   while(it.hasNext()){

     s = s + " " + Integer.toString(it.next());

   }
   System.out.println(s);

 // IMPLEMENT THIS METHOD VIA AN ITERATOR

 // MAKE SURE THE OUTPUT PRODUCED BY THE METHOD
 // DISPLAYS THE LEAST SIGNIFICANT BIT TO THE RIGHT.

  }

在带星号的那一行,我得到以下编译错误。

Error: incompatible types
  required: Iterator
  found:    java.util.Iterator<java.lang.Integer>

我已经试过了,

**Iterator<Integer> it = list.iterator();**

我明白了,Error: type Iterator does not take parameters

编辑 *

我忘了说,我有自己的接口方法实现。

/*
   * Inner class to implement the iterator for the <code>BitList</code>.
   */
  private class BitListIterator implements Iterator {

    /*
     * A reference to the current <code>Node</code> in the iterator. 
     */
    private Node current = null;

    /*
     * The expected modification count variable.
     *  Needed for the "fast-fail" implementation of the iterator.
     */    
    private int expectedModCount = modCount;

    /*
     * Advances the iterator to the next element in the list.
     */
    public int next() {
      checkValid();

      if (current == null) {
        current = first ;
      } else {
        current = current.next ; // move the cursor forward
      }

      if (current == null)
        throw new NoSuchElementException() ;

      return current.value ;
    }

  /**
   * Inserts a new <code>int</code> value immediately before the next element that would be returned by <code>next()</code>.
   */    
    public void add(int newElement) {
      Node newNode;

      if (current == null) {
        first = new Node(newElement, first);
        current = first;
      } else {
        current.next = new Node(newElement, current.next);
        current = current.next;
      }

      modCount++ ;
      expectedModCount++ ;
    }

  /**
   * Indicates whether there is a next element in the list being traversed or not.
   */      
    public boolean hasNext() {
      return ((current == null) && (first != null)) ||
        ((current != null) && (current.next !=null));
    }

  /**
   * Checks whether this iterator remains valid, i.e. no concurrent modifications have been made on the list.
   */      
    private void checkValid() {
      if (expectedModCount != modCount)
        throw new ConcurrentModificationException();
    }
  } // end of BitListIterator class

所以如果导入包我会得到以下错误

Error: BitList.BitListIterator is not abstract and does not override abstract method remove() in java.util.Iterator

Error: next() in BitList.BitListIterator cannot implement next() in java.util.Iterator
  return type int is not compatible with java.lang.Object

我有 jdk 1.7 和它正在使用中。

任何想法都会有所帮助。

谢谢,

Mjall2

【问题讨论】:

  • 编译器是正确的。您还没有实现remove(),并且您对next() 的定义与Iterator 接口中定义的next() 不兼容。如果要使用迭代器类,则需要解决这些问题。
  • list 是什么类型?是BitList吗?当iterator() 被调用时,你是否想让你的BitList 类返回你的BitListIterator 实现?
  • 确保同一目录下没有名为 Iterator.class 的类文件

标签: java generics iterator


【解决方案1】:

我怀疑你的文件顶部没有import java.util.Iterator;

【讨论】:

  • @Mjall2:这看起来是一个完全不相关的问题。您的第一个代码 sn-p 未使用 BitListIterator
  • @Mjall2:你的第一个代码 sn-p 中有“import java.util.Iterator;”吗?
【解决方案2】:

我怀疑你没有用类型声明list。试试这个:

private List<Integer> list = new ArrayList<Integer>();

此外,该消息表明您导入了错误 Iterator 类,如果您在使用 IDE 时不小心,可能会发生这种情况。确保 import java.util.Iterator; 以及没有其他 Iterator 类的导入出现在您的 java 文件的顶部。

【讨论】:

  • 虽然它在代码中并不明确,但我认为list 应该是(尚未看到的)BitList 类的一个实例,@987654327 的存在暗示了它的存在@ 已添加到原始问题中的类。
猜你喜欢
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 2014-09-22
相关资源
最近更新 更多