Merge two array together - Android java.lang

Android examples for java.lang:array union merge

Description

Merge two array together

Demo Code


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

public class Main {
    public static <T> T[] add(T[] src, T[] attach) {
        if (src == null)
            return null;
        if (attach == null)
            return src;

        int src_len = src.length;
        int add_len = attach.length;
        int count = src_len + add_len;
        @SuppressWarnings("unchecked")
        T[] temp = (T[]) Array.newInstance(src.getClass()
                .getComponentType(), count);
        System.arraycopy(src, 0, temp, 0, src_len);
        System.arraycopy(attach, 0, temp, src_len, add_len);
        attach = null;/*w ww.j  ava 2  s  .  c  o m*/
        src = null;
        return temp;
    }

    public static float[] add(float[] src, float[] attach) {
        if (src == null)
            return null;
        if (attach == null)
            return src;

        int src_len = src.length;
        int add_len = attach.length;
        int count = src_len + add_len;
        float[] temp = new float[count];
        System.arraycopy(src, 0, temp, 0, src_len);
        System.arraycopy(attach, 0, temp, src_len, add_len);
        attach = null;
        src = null;
        return temp;
    }
}

Related Tutorials