Java Array Concatenate arrayConcat(String[] first, String second)

Here you can find the source of arrayConcat(String[] first, String second)

Description

Returns a new array adding the second array at the end of first array.

License

Open Source License

Parameter

Parameter Description
first the first array to concatenate
second the array to add at the end of the first array

Return

a new array adding the second array at the end of first array, or null if the two arrays are null.

Declaration

public static final String[] arrayConcat(String[] first, String second) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**// w  w  w .  ja v a2 s  . c  o m
     * Returns a new array adding the second array at the end of first array.
     * It answers null if the first and second are null.
     * If the first array is null or if it is empty, then a new array is created with second.
     * If the second array is null, then the first array is returned.
     * <br>
     * <br>
     * For example:
     * <ol>
     * <li><pre>
     *    first = null
     *    second = "a"
     *    => result = {"a"}
     * </pre>
     * <li><pre>
     *    first = {"a"}
     *    second = null
     *    => result = {"a"}
     * </pre>
     * </li>
     * <li><pre>
     *    first = {"a"}
     *    second = {"b"}
     *    => result = {"a", "b"}
     * </pre>
     * </li>
     * </ol>
     * 
     * @param first the first array to concatenate
     * @param second the array to add at the end of the first array
     * @return a new array adding the second array at the end of first array, or null if the two arrays are null.
     */
    public static final String[] arrayConcat(String[] first, String second) {
        if (second == null)
            return first;
        if (first == null)
            return new String[] { second };

        int length = first.length;
        if (first.length == 0) {
            return new String[] { second };
        }

        String[] result = new String[length + 1];
        System.arraycopy(first, 0, result, 0, length);
        result[length] = second;
        return result;
    }
}

Related

  1. arraycat(String[][] array1, String[][] array2)
  2. arrayConcat(byte[] a, byte[] b)
  3. arrayConcat(final byte[] firstArray, final byte[] secondArray)
  4. arrayConcat(T[] a, T[] b)
  5. arrayConcat(T[] first, T[] second)
  6. arrayConcat(T[] first, T[] second)
  7. arrayConcatenate(final String[] f, final String[] s)