Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: for :each style question Date: Wed, 30 Nov 2011 16:49:44 -0800 Organization: A noiseless patient Spider Lines: 39 Message-ID: References: <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 1 Dec 2011 00:49:44 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="8801"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ZX632TcNSnkGeSpPtFUTJ" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> Cancel-Lock: sha1:BtNUQXPt9TM80IPT1+Sd7MzLL5I= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10389 On 11/30/2011 3: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? public class test { public static void main(String[] args) { int ns[] = new int[23]; int last = 0; for (int i=0; i