Java - Write code to add two string together with separator

Requirements

Write code to add two string together with separator

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String[] x = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        String sep = "book2s.com";
        String[] y = new String[] { "1", "abc", "level", null,
                "book2s.com", "asdf 123" };
        System.out.println(java.util.Arrays.toString(add(x, sep, y)));
    }//from w  w  w  . j ava 2  s .  c  o m

    public static String[] add(String[] x, String sep, String[] y) {
        String[] result = new String[x.length];
        for (int i = 0; i < x.length; i++) {
            result[i] = x[i] + sep + y[i];
        }
        return result;
    }

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

Related Exercise