Re: Bug in ArrayList iterator

2015-01-08 Thread Martin Buchholz
I would not like to add to the overhead of ArrayList's iterator. Those writing performance-critical code are avoiding the iterator and doing for (int i = 0, limit = list.size(); i < limit; i++) { E elt = list.get(i); ... } On Thu, Jan 8, 2015 at 12:11 AM, Remi Forax wrote: > > On 01/08/201

Re: Bug in ArrayList iterator

2015-01-08 Thread Paul Sandoz
> On 01/08/2015 03:24 AM, David Holmes wrote: >> >> So don't do that. > Yes, don't. However, it can happen unintentionally and such an exception helps catch bugs. This can more easily occur when there is some "distance" between iterating and operating on the list. So it's about strengthening

Re: Bug in ArrayList iterator

2015-01-08 Thread Remi Forax
On 01/08/2015 03:24 AM, David Holmes wrote: On 7/01/2015 7:45 PM, Remi Forax wrote: A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); list.add("foo"); list.add("bar"); for(String s: list) { list.remove(s); } :( Rémi tip: the bug lies in Arr

Re: Bug in ArrayList iterator

2015-01-07 Thread David Holmes
On 7/01/2015 7:45 PM, Remi Forax wrote: A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); list.add("foo"); list.add("bar"); for(String s: list) { list.remove(s); } :( Rémi tip: the bug lies in ArrayList.Itr.hasNext() (and AbstractList.Itr.has

Re: Bug in ArrayList iterator

2015-01-07 Thread Remi Forax
On 01/07/2015 11:47 AM, Daniel Fuchs wrote: On 07/01/15 11:31, Paul Sandoz wrote: On Jan 7, 2015, at 10:45 AM, Remi Forax wrote: A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); list.add("foo"); list.add("bar"); for(String s: list) { list.remove

Re: Bug in ArrayList iterator

2015-01-07 Thread Chris Hegarty
On 07/01/15 11:25, Doug Lea wrote: On 01/07/2015 04:45 AM, Remi Forax wrote: A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); list.add("foo"); list.add("bar"); for(String s: list) { list.remove(s); } I have a vague recollection that this i

Re: Bug in ArrayList iterator

2015-01-07 Thread Alan Bateman
On 07/01/2015 11:25, Doug Lea wrote: On 01/07/2015 04:45 AM, Remi Forax wrote: A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); list.add("foo"); list.add("bar"); for(String s: list) { list.remove(s); } I have a vague recollection that this

Re: Bug in ArrayList iterator

2015-01-07 Thread Stanislav Baiduzhyi
On Wednesday 07 January 2015 12:02:42 Chris Hegarty wrote: > > 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 > > war

Re: Bug in ArrayList iterator

2015-01-07 Thread Chris Hegarty
On 07/01/15 11:22, Stanislav Baiduzhyi wrote: 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; +retur

Re: Bug in ArrayList iterator

2015-01-07 Thread Stanislav Baiduzhyi
On Wednesday 07 January 2015 12:29:44 Mario Torre wrote: > > 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 > > warni

Re: Bug in ArrayList iterator

2015-01-07 Thread Paul Sandoz
On Jan 7, 2015, at 11:56 AM, Chris Hegarty wrote: > On 07/01/15 10:47, Daniel Fuchs wrote: >> On 07/01/15 11:31, Paul Sandoz wrote: >>> >>> On Jan 7, 2015, at 10:45 AM, Remi Forax wrote: >>> A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); >>>

Re: Bug in ArrayList iterator

2015-01-07 Thread Mario Torre
2015-01-07 12:22 GMT+01:00 Stanislav Baiduzhyi : > 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 !=

Re: Bug in ArrayList iterator

2015-01-07 Thread Doug Lea
On 01/07/2015 04:45 AM, Remi Forax wrote: A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); list.add("foo"); list.add("bar"); for(String s: list) { list.remove(s); } I have a vague recollection that this issue has come up before, and that no

Re: Bug in ArrayList iterator

2015-01-07 Thread Stanislav Baiduzhyi
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; > >>

Re: Bug in ArrayList iterator

2015-01-07 Thread Pavel Rappo
If I'm not mistaken LinkedList and Vector demonstrate exactly the same buggy behaviour. -Pavel > On 7 Jan 2015, at 11:20, Chris Hegarty wrote: > > On 07/01/15 10:57, Stanislav Baiduzhyi wrote: >> On Wednesday 07 January 2015 10:56:01 Chris Hegarty wrote: >>> public boolean hasNext() {

Re: Bug in ArrayList iterator

2015-01-07 Thread Chris Hegarty
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 pre

Re: Bug in ArrayList iterator

2015-01-07 Thread Stanislav Baiduzhyi
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 skippin

Re: Bug in ArrayList iterator

2015-01-07 Thread Chris Hegarty
On 07/01/15 10:47, Daniel Fuchs wrote: On 07/01/15 11:31, Paul Sandoz wrote: On Jan 7, 2015, at 10:45 AM, Remi Forax wrote: A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); list.add("foo"); list.add("bar"); for(String s: list) { list.remove(s);

Re: Bug in ArrayList iterator

2015-01-07 Thread Daniel Fuchs
On 07/01/15 11:31, Paul Sandoz wrote: On Jan 7, 2015, at 10:45 AM, Remi Forax wrote: A simple Java question, what this code does ? ArrayList list = new ArrayList<>(); list.add("foo"); list.add("bar"); for(String s: list) { list.remove(s); } :( We could improve the best-effo

Re: Bug in ArrayList iterator

2015-01-07 Thread Paul Sandoz
On Jan 7, 2015, at 10:45 AM, Remi Forax wrote: > A simple Java question, what this code does ? > > ArrayList list = new ArrayList<>(); > list.add("foo"); > list.add("bar"); > for(String s: list) { >list.remove(s); > } > > :( > We could improve the best-effort basis by which Concurre

Re: Bug in ArrayList iterator

2015-01-07 Thread Daniel Fuchs
Interesting... I would have expected it to throw java.util.ConcurrentModificationException right away, but it only does so if the list contains exactly 1 or more than 2 elements... best regards, -- daniel On 07/01/15 10:45, Remi Forax wrote: A simple Java question, what this code does ? A

Re: Bug in ArrayList iterator

2015-01-07 Thread Stanislav Baiduzhyi
On Wednesday 07 January 2015 10:45:46 Remi Forax wrote: > A simple Java question, what this code does ? > >ArrayList list = new ArrayList<>(); >list.add("foo"); >list.add("bar"); >for(String s: list) { > list.remove(s); >} > > :( > > Rémi > tip: the bug lies in ArrayList