Mjall2
Mjall2

Reputation: 263

Iterator generics, cannot declare an iterator of mismatching types (maybe casting issue)

I have this simple code

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.

  }

At the line with asterisks, I get the following compilation error.

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

I already tried this,

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

I get, Error: type Iterator does not take parameters

EDIT *

I forgot to mention, I have my own implementation of the interface methods.

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

So if import the package I get the following errors

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

I have jdk 1.7 and its in use.

Any ideas would certainly help.

Thank you,

Mjall2

Upvotes: 0

Views: 3219

Answers (2)

Bohemian
Bohemian

Reputation: 425003

I suspect you haven't declared list with a type. Try this:

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

Also, the message suggests you've imported the wrong Iterator class, which can happen if you aren't careful when using your IDE. Ensure import java.util.Iterator;, and no other import of an Iterator class, appears at the top of your java file.

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272487

I suspect you haven't got import java.util.Iterator; at the top of your file.

Upvotes: 2

Related Questions