dracoblue.net

Implode an Array in Java

Its quite simple to implement the function, you may know from

php, which concats all elements from an array as string and puts a seperator between those.

The following code snippet, takes all elements from inputArray (e.g. 1 2 and 3) and concat them to "1,2,3" and saves this into AsImplodedString.

String AsImplodedString;
if (inputArray.length==0) {
	AsImplodedString = "";
} else {
	StringBuffer sb = new StringBuffer();
	sb.append(inputArray[0]);
	for (int i=1;i<inputArray.length;i++) {
		sb.append(",");
		sb.append(inputArray[i]);
	}
	AsImplodedString = sb.toString();
}

This implementation uses a StringBuffer to be faster with big arrays.

In Java, open source by
@ 2008-04-11, Comments at Reddit & Hackernews