Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10645

Re: for :each style question

From Wanja Gayk <brixomatic@yahoo.com>
Newsgroups comp.lang.java.programmer
Subject Re: for :each style question
Date 2011-12-10 13:28 +0100
Organization Netfront http://www.netfront.net/
Message-ID <MPG.294d6d4544cbb7e09896d5@202.177.16.121> (permalink)
References <30fdd7tlkkejm29ufduhc9nnfk7uu3c6h0@4ax.com> <jb6iv8$8j1$1@dont-email.me> <iyBBq.2059$2e7.1155@newsfe18.iad>

Show all headers | View raw


In article <iyBBq.2059$2e7.1155@newsfe18.iad>, 
newsgroup.nospam@virtualinfinity.net says...

> 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 = ", ";
> }

I always preferred to do it this way:

final StringBuilder builder = new StringBuilder();
final String sep = ", ";
for (final Object o: objects) {
    builder.append(o).append(sep);
}
builder.setLength(Math.max(0,builder.length()-sep.length()));

The only drawback is that the size of the builder's internal buffer may 
grow unnecessarily, if the last separator crosses its current bounds, 
but how often does that happen and how often is it really a problem?

Kind regards,
Wanja


-- 
..Alesi's problem was that the back of the car was jumping up and down 
dangerously - and I can assure you from having been teammate to 
Jean Alesi and knowing what kind of cars that he can pull up with, 
when Jean Alesi says that a car is dangerous - it is. [Jonathan Palmer]

--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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