Java - Write code to Convert a string array to a space-separated string.

Requirements

Write code to Convert a string array to a space-separated string.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String[] arrayString = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(getString(arrayString));
    }/*from  w  w w . j ava2s.  c om*/

    /**
     * Converts a string array to a space-separated string.
     *
     * @param arrayString Specifies the string array to be converted.
     * @return Returns the space-separated string.
     */
    public static String getString(String[] arrayString) {
        if (arrayString == null)
            return (null);

        StringBuffer result = new StringBuffer();
        for (int index = 0; index < arrayString.length; ++index) {
            if (index > 0)
                result.append(" ");

            result.append(arrayString[index].toString());
        }

        return (result.toString());
    }

    /**
     * Appends the second string array to the first one. This is a costly operation as all array members
     * are copied to the result string array.
     *
     * @param firstArray Specifies a string array.
     * @param secondArray Specifies the string array to be appended.
     * @return Returns the new combined string array.
     */
    public static String[] append(String[] firstArray, String[] secondArray) {
        if (firstArray == null)
            return (secondArray);

        if (secondArray == null)
            return (firstArray);

        String[] result = new String[firstArray.length + secondArray.length];
        int index;
        for (index = 0; index < firstArray.length; ++index)
            result[index] = firstArray[index];

        for (index = 0; index < secondArray.length; ++index)
            result[index + firstArray.length] = secondArray[index];

        return (result);
    }
}