Note that there are some explanatory texts on larger screens.

plurals
  1. POIterator.hasNext() goes nuts after remove() from collection
    primarykey
    data
    text
    <p>The code is pretty simple:</p> <pre><code> List lst1 = new ArrayList(){{add("1");}}; Iterator it = lst1.iterator(); System.out.println(it.hasNext()); it.next(); System.out.println(it.hasNext()); lst1.remove(0); System.out.println(it.hasNext()); </code></pre> <p>I know that you should never remove from a collection while iterating through its iterator, please don't tell me that.</p> <p>Instead, please give me a sane explanation why would a remove() operation change the value returned by hasNext() from <em>false</em> to <em>true</em>:</p> <pre><code>$ /usr/lib/jvm/sun-jdk-1.6/bin/java -version java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03) Java HotSpot(TM) Server VM (build 20.1-b02, mixed mode) true false true </code></pre> <p>hasNext() API documentation clearly states that it should return <em>true</em> ONLY if there are more elements in the iterator, and calling next() returns an element. The output proves that it's not the case with SUN JDK 1.6</p> <p>IBM JDK doesn't do that, hasNext returns <em>false</em> after remove() gets called:</p> <pre><code>$ /usr/lib/jvm/ibm-jdk-bin-1.6/bin/java -version java version "1.6.0" Java(TM) SE Runtime Environment (build pxi3260sr9fp2-20110625_01(SR9 FP2)) IBM J9 VM (build 2.4, JRE 1.6.0 IBM J9 2.4 Linux x86-32 jvmxi3260sr9-20110624_85526 (JIT enabled, AOT enabled) true false false </code></pre> <p>Is there a bug in Sun/Oracle hasNext() method? Or, is there a bug in IBM hasNext() method? Or, should I just not bother with all that?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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