Bug in ArrayList iterator

Stanislav Baiduzhyi sbaiduzh at redhat.com
Wed Jan 7 09:49:45 UTC 2015


On Wednesday 07 January 2015 10:45:46 Remi Forax wrote:
> A simple Java question, what this code does ?
> 
>    ArrayList<String> 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.hasNext()).

You would not invoke list.remove(E) when iterating by index, why do you want 
to do that iterating through for-each?

-- 
Regards,
    Stas




More information about the core-libs-dev mailing list