Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #10391
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: for :each style question |
| References | <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> <jb6iv8$8j1$1@dont-email.me> |
| Message-ID | <iyBBq.2059$2e7.1155@newsfe18.iad> (permalink) |
| Date | 2011-11-30 18:23 -0800 |
On 11/30/11 4:49 PM, Knute Johnson wrote:
> 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<ns.length; i++)
> ns[i] = i;
>
> for (int n : ns) {
> last = n;
> }
>
> System.out.println(last);
> }
> }
>
> It's not elegant but it works.
>
for:each is not useful for detecting last, and any acrobatics you go
through to fix it are fairly pointless.
You would also have to handle the case where first=last (and/or is empty).
I find specific use-cases call for different approaches. For instance,
I like the following for string concatenation:
final StringBuilder builder = new StringBuilder();
String sep = "";
for (Object o: objects) {
builder.append(sep).append(o);
sep = ", ";
}
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
for :each style question Roedy Green <see_website@mindprod.com.invalid> - 2011-11-30 15:32 -0800
Re: for :each style question Arne Vajhøj <arne@vajhoej.dk> - 2011-11-30 19:28 -0500
Re: for :each style question Chris Riesbeck <Chris.Riesbeck@gmail.com> - 2011-12-05 14:28 -0600
Re: for :each style question Knute Johnson <nospam@knutejohnson.com> - 2011-11-30 16:49 -0800
Re: for :each style question Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-11-30 18:23 -0800
Re: for :each style question Wanja Gayk <brixomatic@yahoo.com> - 2011-12-10 13:28 +0100
Re: for :each style question Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-12 12:07 -0800
Re: for :each style question Roedy Green <see_website@mindprod.com.invalid> - 2011-12-13 08:08 -0800
Re: for :each style question Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-13 12:07 -0800
Re: for :each style question Wanja Gayk <brixomatic@yahoo.com> - 2011-12-17 16:20 +0100
Re: for :each style question Jim Janney <jjanney@shell.xmission.com> - 2011-12-14 14:31 -0700
Re: for :each style question Lew <lewbloch@gmail.com> - 2011-12-14 16:53 -0800
Re: for :each style question Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-15 16:16 -0800
Re: for :each style question Wanja Gayk <brixomatic@yahoo.com> - 2011-12-17 12:31 +0100
Re: for :each style question Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-30 21:35 -0500
Re: for :each style question Lew <lewbloch@gmail.com> - 2011-11-30 19:12 -0800
Re: for :each style question Robert Klemme <shortcutter@googlemail.com> - 2011-12-01 23:36 +0100
Re: for :each style question Steven Simpson <ss@domain.invalid> - 2011-12-04 19:43 +0000
csiph-web