Bug in ArrayList iterator

Mario Torre neugens.limasoftware at gmail.com
Wed Jan 7 11:29:44 UTC 2015


2015-01-07 12:22 GMT+01:00 Stanislav Baiduzhyi <sbaiduzh at redhat.com>:
> On Wednesday 07 January 2015 11:20:57 you wrote:
>> On 07/01/15 10:57, Stanislav Baiduzhyi wrote:
>> > On Wednesday 07 January 2015 10:56:01 Chris Hegarty wrote:
>> >>            public boolean hasNext() {
>> >>
>> >> -            return cursor != size;
>> >> +            return cursor != itrSize;
>> >>
>> >>            }
>> >
>> > If the user will invoke list.remove(E) to remove current or previous
>> > element then iterator will be skipping some elements.
>>
>> If this happens, then next() and/or remove() will throw CME.
>>
>>   Throwing ConcurrentModification
>>
>> > in hasNext() looks better in such case.
>>
>> It is not clear to me that users would be expecting CME from hasNext(),
>> whereas it is more next() and remove() is more obvious.
>
> Yes, but that exactly how it works now, that's why second invocation of
> hasNext() returns false and we can see the issue Remi is talking about. If
> hasNext() will be throwing ConcurrentModification than will work as early
> warning system that something is wrong with this iterator.

But hasNext isn't modifying things, why should it throw
ConcurrentModification? I would find it very confusing, and also
likely non backward compatible.

Cheers,
Mario

-- 
pgp key: http://subkeys.pgp.net/ PGP Key ID: 80F240CF
Fingerprint: BA39 9666 94EC 8B73 27FA  FC7C 4086 63E3 80F2 40CF

Java Champion - Blog: http://neugens.wordpress.com - Twitter: @neugens
Proud GNU Classpath developer: http://www.classpath.org/
OpenJDK: http://openjdk.java.net/projects/caciocavallo/

Please, support open standards:
http://endsoftpatents.org/



More information about the core-libs-dev mailing list