Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Chris Riesbeck Newsgroups: comp.lang.java.programmer Subject: Re: for :each style question Date: Mon, 05 Dec 2011 14:28:41 -0600 Lines: 41 Message-ID: <9k4nrpFssU1@mid.individual.net> References: <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> <4ed6ca3e$0$291$14726298@news.sunsite.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net /qAbTPwl9OPxexjsrCTrbg6mIF5sqsoSmjj2sD+9zkKpmEPQCk Cancel-Lock: sha1:BMKCCZxwJ7bxNFv4F5R3zRk1yS4= User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <4ed6ca3e$0$291$14726298@news.sunsite.dk> Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10534 On 11/30/2011 6:28 PM, Arne Vajhøj wrote: > On 11/30/2011 6:32 PM, 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? > > first > for first+1 to last-1 > last > > Not much point in for(each) when it in fact is not for each. > While it's not a Java for:each loop, it's well within the concept of for each loops in general. The JSTL foreach element has some optional syntax for accessing just this kind of data : ... Inside the forEach, status can tell you if you're first, last, etc. http://www.ibm.com/developerworks/java/library/j-jstl0318/ There are a couple of things I wish Java's for:each did. The other biggie is looping across several collections in parallel.