Android Array Concatenate concat(T[] first, T[]... rest)

Here you can find the source of concat(T[] first, T[]... rest)

Description

concat

License

Open Source License

Declaration

public static <T> T[] concat(T[] first, T[]... rest) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Array;

public class Main {
    public static <T> T[] concat(T[] first, T[]... rest) {
        int totalLength = first.length;
        for (T[] arr : rest) {
            totalLength += arr.length;//  w ww.j a  v  a  2s  .c o m
        }

        T[] result = (T[]) Array.newInstance(first.getClass(), totalLength);
        System.arraycopy(first, 0, result, 0, first.length);
        int pos = first.length;

        for (T[] arr : rest) {
            System.arraycopy(arr, 0, result, pos, arr.length);
            pos += arr.length;
        }

        return result;
    }

    public static byte[] concat(byte[] first, byte[]... rest) {
        int totalLength = first.length;
        for (byte[] arr : rest) {
            totalLength += arr.length;
        }

        byte[] result = new byte[totalLength];
        System.arraycopy(first, 0, result, 0, first.length);
        int pos = first.length;

        for (byte[] arr : rest) {
            System.arraycopy(arr, 0, result, pos, arr.length);
            pos += arr.length;
        }

        return result;
    }
}

Related

  1. concat(byte[] first, byte[]... rest)
  2. cancat(byte[] a, byte[] b)
  3. arrayApend(byte[] prep, byte after)
  4. arrayComb(byte[] prep, byte[] after)