On 7 January 2011 16:36, Eric Jablow <[email protected]> wrote: > > 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. > > That's no good for parallel execution however. The Separator class is mutable (explicitly depending on mutation as a side-effect to work at all) and so relies on order of execution.
You'd be better off adding a separator to the end of every element, then deleting it again from the end of the final concatenated value. -- Kevin Wright gtalk / msn : [email protected] <[email protected]>mail: [email protected] vibe / skype: kev.lee.wright twitter: @thecoda -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
