Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #10492
| From | Steven Simpson <ss@domain.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: for :each style question |
| Date | 2011-12-04 19:43 +0000 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <m3vsq8-45n.ln1@news.simpsonst.f2s.com> (permalink) |
| References | <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> |
On 30/11/11 23:32, Roedy Green wrote:
> In a for:each loop, sometimes you want to treat the first and or last
> element specially. [...]
> What do you consider the best style to deal with this?
If I needed it a lot...
import java.util.Arrays;
import java.util.List;
public class FirstLastLoop {
interface FirstLastBlock<T> {
void invoke(T t, boolean isFirst, boolean isLast);
}
static<T> void forEach(Iterable<? extends T> coll,
FirstLastBlock<? super T> block) {
boolean isFirst = true;
boolean lastHeld = false;
T last = null;
for (T t : coll) {
if (lastHeld) {
block.invoke(last, isFirst, false);
isFirst = false;
}
last = t;
lastHeld = true;
}
if (lastHeld)
block.invoke(last, isFirst, true);
}
public static void main(String[] args) throws Exception {
List<String> list = Arrays.asList(args);
FirstLastBlock<String> block = new FirstLastBlock<String>() {
public void invoke(String t, boolean isFirst, boolean isLast) {
if (isFirst)
System.out.print("The list: ");
else if (isLast)
System.out.print(" and ");
else
System.out.print(", ");
System.out.print(t);
if (isLast)
System.out.println('.');
}
};
forEach(list, block);
// Lambda version
forEach(list, (t, isFirst, isLast) -> {
if (isFirst)
System.out.print("The list: ");
else if (isLast)
System.out.print(" and ");
else
System.out.print(", ");
System.out.print(t);
if (isLast)
System.out.println('.');
});
}
}
--
ss at comp dot lancs dot ac dot uk
Back to comp.lang.java.programmer | Previous | Next — Previous 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