Java - Write code to Convert an array of string elements into a comma-delimited string.

Requirements

Write code to Convert an array of string elements into a comma-delimited string.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String[] array = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(toCommaDelimited(array));
    }/*from w  w  w .  j  a  v a2s .  c o  m*/

    /**
     * Convert an array of string elements into a comma-delimited string.
     * <p>
     * Example:  ["cat","dog","kangaroo"] -> "cat,dog,kangaroo"
     * 
     * @param array array of string elements
     * @return comma-delimited elements as string
     */
    public static String toCommaDelimited(String[] array) {
        StringBuffer buf;

        buf = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            buf.append(array[i]);
            if (i < array.length - 1)
                buf.append(",");
        }
        return buf.toString();
    }
}