Java Array Concatenate concatArrays(T[] first, T[] second)

Here you can find the source of concatArrays(T[] first, T[] second)

Description

Concatenate two arrays and return the result.

License

Open Source License

Parameter

Parameter Description
T The type of both arrays.
first The first array.
second The second array.

Return

An array consisting of the concatenation of first and second. That is,
\forall i : 0 <= i < first.length : \result[i] == first[i]
and analoguous
\forall i : first.length <= i < \result.length : \result[i] == second[i - first.length]
.
If one the two is null , the other array will be returned. If both are null , then null will be returned.

Declaration

public static <T> T[] concatArrays(T[] first, T[] second) 

Method Source Code

//package com.java2s;
/*//from w  w  w. j a v  a2  s. c o m
 * Copyright 2013  Thom Castermans  thom.castermans@gmail.com
 * Copyright 2013  Willem Sonke     willemsonke@planet.nl
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License or (at your option) version 3 or any later version
 * accepted by the membership of KDE e.V. (or its successor approved
 * by the membership of KDE e.V.), which shall act as a proxy 
 * defined in Section 14 of version 3 of the license.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;

public class Main {
    /**
     * Concatenate two arrays and return the result.
     * This method is copied from <a href="http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java">
     * StackOverflow</a>, answer of <i>Joachim Sauer</i> and then slightly adjusted.
     * 
     * @param  <T>    The type of both arrays.
     * @param  first  The first array.
     * @param  second The second array.
     * @return An array consisting of the concatenation of first and second.
     *          That is, <pre>\forall i : 0 <= i < first.length : \result[i] == first[i]</pre>
     *         and analoguous <pre>\forall i : first.length <= i < \result.length : \result[i] == second[i - first.length]</pre>.<br>
     *         If one the two is {@code null}, the other array will be returned.
     *         If both are {@code null}, then {@code null} will be returned.
     */
    public static <T> T[] concatArrays(T[] first, T[] second) {
        if (first == null && second == null)
            return null;
        if (first == null)
            return second;
        if (second == null)
            return first;
        T[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }
}

Related

  1. concatArrays(Object[] ar1, Object[] ar2)
  2. concatArrays(short[] arr1, short[] arr2)
  3. concatArrays(String[] A, String[] B)
  4. concatArrays(String[] array, String[] arrayToBeConcat)
  5. concatArrays(T[] first, T[] second)
  6. concatByteArrays(final byte[] array1, final byte[] array2)
  7. concatenate(@SuppressWarnings("unchecked") T[]... arrays)
  8. concatenate(byte[] a, byte[] b)
  9. concatenate(double[] p1, double[] p2)