Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: for :each style question Date: Wed, 30 Nov 2011 19:12:15 -0800 (PST) Organization: http://groups.google.com Lines: 24 Message-ID: <3484881.48.1322709135595.JavaMail.geo-discussion-forums@prdv33> References: <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 67.218.107.93 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1322709136 21478 127.0.0.1 (1 Dec 2011 03:12:16 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 1 Dec 2011 03:12:16 +0000 (UTC) In-Reply-To: <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=67.218.107.93; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10396 Roedy Green wrote: > In a for:each loop, sometimes you want to treat the first and or last > element specially. > > The obvious way to handle is to revert to a for int i= loop and check > for special values of i. > > You can keep the for:each style if you have a boolean first= true that > you set false to detect the first. > > I don't know of an equivalent way to detect the last. > > In the olden days I would have handled the first and last cases > outside the loop, with the loop running over the middle elements. You > can't do that with for:each. > > What do you consider the best style to deal with this? Regular 'for' loop (or its 'while' or 'do...while' cousins). For:each is for a particular use case where it helps. For other use cases, you probably shouldn't use it. -- Lew