Note that there are some explanatory texts on larger screens.

plurals
  1. POIterator generics, cannot declare an iterator of mismatching types (maybe casting issue)
    text
    copied!<p>I have this simple code</p> <pre><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. } </code></pre> <p>At the line with asterisks, I get the following compilation error.</p> <pre><code>Error: incompatible types required: Iterator found: java.util.Iterator&lt;java.lang.Integer&gt; </code></pre> <p>I already tried this,</p> <pre><code>**Iterator&lt;Integer&gt; it = list.iterator();** </code></pre> <p>I get, <code>Error: type Iterator does not take parameters</code></p> <p>EDIT <em>*</em></p> <p>I forgot to mention, I have my own implementation of the interface methods.</p> <pre><code>/* * Inner class to implement the iterator for the &lt;code&gt;BitList&lt;/code&gt;. */ private class BitListIterator implements Iterator { /* * A reference to the current &lt;code&gt;Node&lt;/code&gt; 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 &lt;code&gt;int&lt;/code&gt; value immediately before the next element that would be returned by &lt;code&gt;next()&lt;/code&gt;. */ 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) &amp;&amp; (first != null)) || ((current != null) &amp;&amp; (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 </code></pre> <p>So if import the package I get the following errors</p> <pre><code>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 </code></pre> <p>I have jdk 1.7 and its in use.</p> <p>Any ideas would certainly help.</p> <p>Thank you,</p> <p>Mjall2</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload