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
@ 11 Apr 2008, Comments at Reddit & Hackernews

Give something back

Were my blog posts useful to you? If you want to give back, support one of these charities, too!

Report hate in social media Campact e.V. With our technology and your help, we protect the oceans from plastic waste. Gesellschaft fur Freiheitsrechte e. V. The civil eye in the mediterranean

Recent Dev-Articles

Read recently

Recent Files

About