首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Iterator泛型,不能声明不匹配类型的迭代器(可能是浇铸问题)

Iterator泛型,不能声明不匹配类型的迭代器(可能是浇铸问题)
EN

Stack Overflow用户
提问于 2012-04-11 00:37:44
回答 2查看 3K关注 0票数 0

我有一个简单的代码

代码语言:javascript
复制
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.

  }

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

代码语言:javascript
复制
Error: incompatible types
  required: Iterator
  found:    java.util.Iterator<java.lang.Integer>

我已经试过了

代码语言:javascript
复制
**Iterator<Integer> it = list.iterator();**

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

编辑*

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

代码语言:javascript
复制
/*
   * 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

因此,如果导入包,我将得到以下错误

代码语言:javascript
复制
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

我有JDK1.7并在使用它。

任何想法都会有帮助。

谢谢,

Mjall2

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-11 00:47:53

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

代码语言:javascript
复制
private List<Integer> list = new ArrayList<Integer>();

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

票数 1
EN

Stack Overflow用户

发布于 2012-04-11 00:39:50

我怀疑你没有把import java.util.Iterator;放在文件的顶端。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10098391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档