commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Todd V. Jonker" <t...@consciouscode.com>
Subject RE: [lang] Array concat?
Date Wed, 04 Feb 2004 19:48:18 GMT
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"
<tim.reilly@consultant.com> 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: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message