Path: csiph.com!aioe.org!.POSTED!not-for-mail From: =?UTF-8?Q?Arne_Vajh=c3=b8j?= Newsgroups: comp.lang.java.programmer Subject: Re: Removing an element while iterating over a List with enhanced for? Date: Thu, 16 Feb 2017 14:02:21 -0500 Organization: Aioe.org NNTP Server Lines: 27 Message-ID: References: NNTP-Posting-Host: PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.1 X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.lang.java.programmer:36296 On 2/16/2017 12:30 PM, 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. :-) Note though that Java 9: list = List.of(1, 5, 7, 9, 13); will not work! Arne