Java Array Merge mergeArrays(byte[] first, byte[]... more)

Here you can find the source of mergeArrays(byte[] first, byte[]... more)

Description

Merges two or more byte arrays into one.

License

Open Source License

Parameter

Parameter Description
first The first array to be placed into the new array.
more More byte arrays to merge.

Return

A new byte array containing the data of every specified array.

Declaration

public static byte[] mergeArrays(byte[] first, byte[]... more) 

Method Source Code


//package com.java2s;
// See LICENSE.txt for license information

import java.util.Arrays;

public class Main {
    /**//from  w w w.  j a  va2s  .co  m
     * Merges two or more byte arrays into one.
     * @param first The first array to be placed into the new array.
     * @param more More byte arrays to merge.
     * @return A new byte array containing the data of every specified array.
     */
    public static byte[] mergeArrays(byte[] first, byte[]... more) {
        int totalLength = first.length;
        for (byte[] ar : more) {
            totalLength += ar.length;
        }

        byte[] res = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (byte[] ar : more) {
            System.arraycopy(ar, 0, res, offset, ar.length);
            offset += ar.length;
        }

        return res;
    }

    /**
     * Merges two or more generic arrays of the same type into one.
     * Note: The result for arrays of different types but common base type is undefined.
     * @param first The first array to be placed into the new array.
     * @param more More arrays of the same type to merge.
     * @return A new array containing the data of all specified arrays.
     */
    public static <T> T[] mergeArrays(T[] first, T[]... more) {
        int totalLength = first.length;
        for (T[] ar : more) {
            totalLength += ar.length;
        }

        T[] res = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (T[] ar : more) {
            System.arraycopy(ar, 0, res, offset, ar.length);
            offset += ar.length;
        }

        return res;
    }
}

Related

  1. mergeArray(String[] a, String[] b)
  2. mergeArray(T[] objs, String concatenator)
  3. mergeArrayIntoString(final Object[] array, String middleDelimiter, String lastDelimiter)
  4. mergeArrayObject(Object[] buf1, Object[] buf2)
  5. mergeArrays(byte[] buf1, byte[] buf2)
  6. mergeArrays(final byte[] buf1, final byte[] buf2)
  7. mergeArrays(final Object[] a1, final Object[] a2, final Object[] merge)
  8. mergeArrays(int[] theArrayA, int[] theArrayB)
  9. mergeArrays(long[] alldistances, long[] currentUserDistances)