On Jan 7, 8:22 am, Casper Bang <casper.b...@gmail.com> wrote: > ... > We've all learned that we should avoid polluting scope with temporary > variables and exposed state, which is why we should favor iterators > over indexing. However, there are a great deal of times where we need > the index either directly or indirectly when i.e. operating of > multiple aligned structures, treating the first item specially, or the > last, etc. >... Well, there is this trick invented by someone at the Technion, I think. public class Separator { private final String sep; private String curSep = ""; public Separator(final String sep) { this.sep = sep; } public String toString() { String out = curSep; curSep = sep; return out; } }
--- String join (Iterable<String> iterable, String sep) { Separator separator = new Separator(sep); StringBuilder b = new StringBuilder(); for (String s: iterable) { b.append(separator).append(s); } return b.toString(); } --- The exercise is to extend this idea to one for more general handling of the first element. Respectfully, Eric Jablow -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to javapo...@googlegroups.com. To unsubscribe from this group, send email to javaposse+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.