Combine the contents of two arrays and return the result. - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

Combine the contents of two arrays and return the result.

Demo Code


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

public class Main {
    /**/* ww w.  j a  v a2 s  .c  o  m*/
     * Combine the contents of two arrays and return the result. Array B will be appended onto array A
     * @param a The first array
     * @param b The second array
     * @param <T> The types of these arrays
     * @return An array with the contents of both A and B
     */
    public static <T> T[] combine(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