Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Removing an element while iterating over a List with enhanced for? Date: Thu, 16 Feb 2017 23:21:30 +0100 Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net d9t1J+YkajMO+XTKhVybCQHwoKDeA6byigMBxljDA2B4IyNi4= Cancel-Lock: sha1:tn2NZ09LJ2tB8R223hsWWPWZHwg= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:36313 On 16.02.2017 21:09, Volker Borchert wrote: > Robert Klemme wrote: >> On 16.02.2017 18:17, Knute Johnson wrote: >>> On 2/15/2017 21:43, Arne Vajh??j wrote: >>>> 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. >>>> >>>>> List list = Stream.of(1,5,7,9,13).collect(toList()); >> >> Btw. I know it is just the code to get a list filled but still you can >> have that much simpler: >> >> list = Arrays.asList(1, 5, 7, 9, 13); >> >> We tend to forget the "good ole code" over all the fancy new things. :-) > > The list you get from this does not support removal. Correct. Slightly less simple list = new ArrayList<>(Arrays.asList(1, 5, 7, 9, 13)); I concede it's not so nice any more. :-) Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/