Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36276

Re: Removing an element while iterating over a List with enhanced for?

Path csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Removing an element while iterating over a List with enhanced for?
Date Wed, 15 Feb 2017 18:24:04 -0500
Organization A noiseless patient Spider
Lines 51
Message-ID <o82nr7$5me$1@dont-email.me> (permalink)
References <o82m7a$tls$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Wed, 15 Feb 2017 23:22:15 -0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="78beeea5faa7d3f71f0f2901573c1d7c"; logging-data="5838"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+40qBcj2rdCNK5nJHiZ5yG"
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1
In-Reply-To <o82m7a$tls$1@dont-email.me>
Cancel-Lock sha1:+znRngrBRM4Q1IqH+CubB8gr7qY=
Xref csiph.com comp.lang.java.programmer:36276

Show key headers only | View raw


On 2/15/2017 5:56 PM, Knute Johnson wrote:
> An enhanced for loop on a List is no different than using an Iterator
> right?  So is there any problem removing the current element from the
> list while using the enhanced for loop?  I couldn't find anything that
> said it was OK or not OK in the JLS.

     1) During an iteration, you can only remove elements through
the Iterator's own remove() method.

     2) The enhanced `for' does indeed use an Iterator, but it
doesn't expose the Iterator instance: You have no way to refer
to it.

     3) Since you can't get a reference to the Iterator, you can't
call the Iterator's remove().

     Therefore,

     4) You can't remove anything while executing an enhanced `for'.

>         List<Integer> list = Stream.of(1,5,7,9,13).collect(toList());
>
>         for (Integer i : list)
>             if (i == 9)
>                 list.remove(i);

     Not permitted.  From the Javadoc for Iterator#remove: "The behavior
of an iterator is unspecified if the underlying collection is modified
while the iteration is in progress in any way other than by calling
this method."

     Just for fun, I tried your example -- and it worked!!!  But that's
one of the possible outcomes of "unspecified" behavior, and I rather
suspected that it "worked" because the removed element just happened
to be the penultimate and the Iterator's hasNext() might have returned
false.  So I changed `9' to `7' to attack an earlier element, and got

Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
	at java.util.ArrayList$Itr.next(ArrayList.java:851)
	at esosman.misc.Knute.main(Knute.java:15)

>         while (iter.hasNext())
>             if (iter.next() == 9)
>                 iter.remove();

     This is the correct approach.

-- 
esosman@comcast-dot-net.invalid
One thousand four hundred thirty-five days to go.

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Removing an element while iterating over a List with enhanced for? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-02-15 16:56 -0600
  Re: Removing an element while iterating over a List with enhanced for? Eric Sosman <esosman@comcast-dot-net.invalid> - 2017-02-15 18:24 -0500
    Re: Removing an element while iterating over a List with enhanced for? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-02-15 17:51 -0600
  Re: Removing an element while iterating over a List with enhanced for? Arne Vajhøj <arne@vajhoej.dk> - 2017-02-15 22:43 -0500
    Re: Removing an element while iterating over a List with enhanced for? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-02-16 11:17 -0600
      Re: Removing an element while iterating over a List with enhanced for? Robert Klemme <shortcutter@googlemail.com> - 2017-02-16 18:30 +0100
        Re: Removing an element while iterating over a List with enhanced for? Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2017-02-16 11:35 -0600
        Re: Removing an element while iterating over a List with enhanced for? Arne Vajhøj <arne@vajhoej.dk> - 2017-02-16 14:02 -0500
        Re: Removing an element while iterating over a List with enhanced for? v_borchert@despammed.com (Volker Borchert) - 2017-02-16 20:09 +0000
          Re: Removing an element while iterating over a List with enhanced for? Robert Klemme <shortcutter@googlemail.com> - 2017-02-16 23:21 +0100

csiph-web