0

私はこの簡単なコードを持っています

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を持っており、それを使用しています。

どんなアイデアでも確かに役立ちます。

ありがとうございました、

ミヤル2

4

2 に答える 2

2

import java.util.Iterator;ファイルの先頭に達していないと思われます。

于 2012-04-11T00:39:50.620 に答える
1

型で宣言していないと思われますlist。これを試して:

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

また、このメッセージは、間違った クラスをインポートしたことを示唆しています。これはIterator、IDE の使用時に注意を怠ると発生する可能性があります。import java.util.Iterator;Java ファイルの先頭に が表示され、Iterator クラスの他のインポートが表示されないことを確認します。

于 2012-04-11T00:47:53.457 に答える