java - 迭代器泛型,无法声明不匹配类型的迭代器(可能是转换问题)

标签 java generics iterator

我有这个简单的代码

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();**

我明白了,错误:类型迭代器不接受参数

编辑*

我忘了提及,我有自己的接口(interface)方法实现。

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

最佳答案

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

关于java - 迭代器泛型,无法声明不匹配类型的迭代器(可能是转换问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098391/

相关文章:

java - 为什么类不是 Java 中的第一类对象?

java - 泛型方法实现中的不同返回值类型

c++ - STL list<Object> 迭代器用于更改列表中的对象

c++ - 非常奇怪的卡在 for 循环初始化

c++ - 比较不同类型的 C++ for 循环

Java gui框架的第二个函数

Java 奇怪的泛型返回类型

java - 无法在 JPanel 上绘图

java - 在Java中,当另一个音频文件开始使用key_events时,如何停止前一个音频文件

java - 不同帧率游戏版本中LibGDX的差异