Java Array Concatenate concat(T[]... arrays)

Here you can find the source of concat(T[]... arrays)

Description

Concatenates an array of generic arrays

License

Apache License

Parameter

Parameter Description
arrays The arrays being concatenated

Return

The concatenated array

Declaration

@SafeVarargs
public static <T> T[] concat(T[]... arrays) 

Method Source Code


//package com.java2s;
/*/* www.  j a  va 2  s  . c om*/
 * Copyright 2018 ImpactDevelopment
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**
     * Concatenates an array of generic arrays
     *
     * @param arrays The arrays being concatenated
     * @return The concatenated array
     */
    @SafeVarargs
    public static <T> T[] concat(T[]... arrays) {
        if (arrays.length < 2)
            throw new IllegalArgumentException("At least 2 arrays should be supplied");

        T[] result = arrays[0];
        for (int i = 1; i < arrays.length; i++) {
            T[] newArray = Arrays.copyOf(result, result.length + arrays[i].length);
            System.arraycopy(arrays[i], 0, newArray, result.length, arrays[i].length);
            result = newArray;
        }

        return result;
    }
}

Related

  1. concat(T[] first, T[] second)
  2. concat(T[] first, T[]... rest)
  3. concat(T[] head, T... tail)
  4. concat(T[] left, T[] right)
  5. concat(T[] objects)
  6. concat(T[]... tss)
  7. concatAll(byte[] a, byte[]... rest)
  8. concatAll(byte[] first, byte[]... rest)
  9. concatAll(byte[]... args)