concatenate two generic array - Android java.lang

Android examples for java.lang:array union merge

Description

concatenate two generic array

Demo Code

import java.lang.reflect.Array;

public class Main {

  public static <T> T[] concat(T[] A, T[] B) {
    final Class<?> typeofA = A.getClass().getComponentType();
    @SuppressWarnings("unchecked")
    T[] C = (T[]) Array.newInstance(typeofA, A.length + B.length);
    System.arraycopy(A, 0, C, 0, A.length);
    System.arraycopy(B, 0, C, A.length, B.length);

    return C;/* ww  w .  java 2 s . com*/
  }

}

Related Tutorials