Friday, September 28, 2012

Append two Arrays in Java

Still need to optimize this method but this works with out any issues,

public String[] appendArrays(String[] arrayToAdd, String[] arrayFromAdd) {
        List<String> list = new ArrayList<String>(Arrays.asList(arrayToAdd));
        if (arrayFromAdd != null && arrayFromAdd.length > 0) {
            list.addAll(Arrays.asList(arrayFromAdd));
        } else {
            arrayFromAdd = new String[]{"N/A"};
            list.addAll(Arrays.asList(arrayFromAdd));
        }
        return list.toArray(new String[list.size()]);
    }