Concatenate 2 arrays into 1. - Java java.lang

Java examples for java.lang:int Array

Description

Concatenate 2 arrays into 1.

Demo Code


//package com.java2s;
import java.lang.reflect.Array;

public class Main {
    /**//w ww . j a v a 2s  . c om
     * Concatenate 2 arrays into 1.
     */
    public static <T> T[] concatenate(T[] a, T... b) {
        int aLen = a.length;
        int bLen = b.length;

        @SuppressWarnings("unchecked")
        T[] C = (T[]) Array.newInstance(a.getClass().getComponentType(),
                aLen + bLen);
        System.arraycopy(a, 0, C, 0, aLen);
        System.arraycopy(b, 0, C, aLen, bLen);

        return C;
    }
}

Related Tutorials