Straight from one of my existing utilz classes
C:\java\home\src\krc\utilz\Arrayz.java
package krc.utilz;
/**
* A bunch of static helper methods for arrays of String's.
* @See also krc.utilz.IntArrays for arrays of int's.
*/
public abstract class Arrayz
{
/**
* Concetenates the values in the given array into a string, seperated by FS.
* @param FS String - Field Seperator - Name borrowed from awk
* @param Object[] a - array to be concatentated
* @return a string representation of the given array.
*/
public static String join(String FS, Object[] a) {
if (a==null||a.length==0) return "";
StringBuilder result = new StringBuilder(String.valueOf(a[0]));
for(int i=1; i<a.length; i++) {
result.append(FS);
result.append(String.valueOf(a[i]));
}
return result.toString();
}
....
}
Cheers. Keith.
EDIT
Here's a quick & dirty performance comparison, using java.util.Arrays as a baseline.
Note that hotspot cost is amortized over 100 iterations, and should be (more or less) the same for all three techniques... krc.utilz.RandomString and krc.utilz.Arrayz are both available upon request, just ask.
package forums;
import java.util.Arrays;
import krc.utilz.Arrayz;
import krc.utilz.RandomString;
class ArrayToStringPerformanceTest
{
private static final int NS2MS = 1000000; // 1 millisecond (1/10^3) = 1,000,000 nanoseconds (1/10^9)
public static void main(String[] args) {
try {
String[] array = randomStrings(100*1000, 16);
long start, stop;
String result;
final int TIMES = 100;
long time1=0L, time2=0L, time3=0L;
for (int i=0; i<TIMES; i++) {
start = System.nanoTime();
result = Arrays.toString(array);
stop = System.nanoTime();
//System.out.println("Arrays.toString took "+(stop-start)+" ns");
time1 += (stop-start);
start = System.nanoTime();
result = Arrayz.join(", ", array);
stop = System.nanoTime();
//System.out.println("Arrayz.join took "+(stop-start)+" ns");
time2 += (stop-start);
start = System.nanoTime();
result = arrayToString(array, ", ");
stop = System.nanoTime();
//System.out.println("arrayToString took "+(stop-start)+" ns");
time3 += (stop-start);
}
System.out.format("java.util.Arrays.toString took "+(time1/TIMES/NS2MS)+" ms");
System.out.format("krc.utilz.Arrayz.join took "+(time2/TIMES/NS2MS)+" ms");
System.out.format("arrayToString took "+(time3/TIMES/NS2MS)+" ms");
} catch (Exception e) {
e.printStackTrace();
}
}
public static String arrayToString(String[] array, String spacer) {
StringBuffer result = new StringBuffer();
for ( int i=0; i<array.length; i++ ) {
result.append( array[i] + ((i+1<array.length)?spacer:"") );
}
return result.toString();
}
private static String[] randomStrings(int howMany, int length) {
RandomString random = new RandomString();
String[] a = new String[howMany];
for ( int i=0; i<howMany; i++) {
a[i] = random.nextString(length);
}
return a;
}
}
/*
C:\Java\home\src\forums>"C:\Program Files\Java\jdk1.6.0_12\bin\java.exe" -Xms512m -Xmx1536m -enableassertions -cp C:\Java\home\classes forums.ArrayToStringPerformanceTest
java.util.Arrays.toString took 26 ms
krc.utilz.Arrayz.join took 32 ms
arrayToString took 59 ms
*/
See also Doomspork's suggestion, and my comment thereon.
Cheers. Keith.