Hi Todd,

You're right, that's much more efficient.

Stephen pointed out my suggestion is mute since StringUtils.join implements
the method (also pre-sizes the buffer.) But thanks for the suggestion.

-TR

> -----Original Message-----
> From: Todd V. Jonker [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, February 04, 2004 2:48 PM
> To: Jakarta Commons Developers List
> Subject: RE: [lang] Array concat?
>
>
> It might be worth iterating through array[] once to calculate the final
> string size, so you can preallocate the StringBuffer and avoid having to
> expand it while you append.  This keeps the algorithm running in O(n)
> time as opposed to O(n*log(n)) or something.
>
> .T.
>
>
>
> On Fri, 30 Jan 2004 19:52:30 -0500, "Tim Reilly"
> <[EMAIL PROTECTED]> said:
>
> > Any thoughts on using:
> > //------------------------
> >     /**
> >      * <p>Returns a new string by converting each element of the passed
> > array
> >      * using the element's toString method and inserts the given
> >      delimiter
> >      * between each element.</p>
> >      *
> >      * <pre>
> >      * Object[] anArray = {"Apples", "Bananas", "Oranges"};
> >      * ArrayUtil.join(anArray, ", ");
> >      * Gives us;
> >      * Apples, Bananas, Oranges
> >      * </pre>
> >      *
> >      * @param array The array who's elements should be joined into a
> >      String.
> >      * @param delim The delimiter to insert between each element.
> >      * @return a new String constructed by calling toString on
> each array
> >      *          element and inserting the passed delimiter.
> >      */
> >     public static String join(Object[] array, String delim) {
> >         StringBuffer buf = new StringBuffer();
> >         for(int i = 0; i < array.length; i++) {
> >             buf.append(
> >                 ((array[i] != null)? array[i].toString():"null")
> >                     + ((i == array.length-1)? "":delim));
> >         }
> >         return buf.toString();
> >     }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to