Java Array Concatenate concat(T[] array, Collection collection)

Here you can find the source of concat(T[] array, Collection collection)

Description

concat

License

Open Source License

Parameter

Parameter Description
conditions a parameter
authConditions a parameter

Declaration

@SuppressWarnings("unchecked")
public static <T> T[] concat(T[] array, Collection<T> collection) 

Method Source Code


//package com.java2s;
/*//from   w  w w. j a  v a  2 s.  co  m
  This file is part of Cuber.
    
Cuber is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
Cuber is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Cuber.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Arrays;
import java.util.Collection;

public class Main {
    /**
     * @param conditions
     * @param authConditions
     * @return
     * @author wonka
     * @since 05/04/2013
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] concat(T[] array, Collection<T> collection) {
        if (collection == null || collection.isEmpty()) {
            return array;
        }
        return concat(array, (T[]) collection.toArray());
    }

    /**
     * @param first
     * @param rest
     * @return resulting array.
     * @author wonka
     * @since 05/04/2013
     */
    @SafeVarargs
    public static <T> T[] concat(T[] first, T[]... rest) {
        if (rest == null || rest.length == 0) {
            return first;
        }
        int totalLength = first.length;
        for (T[] array : rest) {
            totalLength += array.length;
        }
        T[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (T[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }
}

Related

  1. concat(String[]... arrays)
  2. concat(T val, T[] vals)
  3. concat(T[] a, T[] b)
  4. concat(T[] a, T[] b, T[] c, T[] d)
  5. concat(T[] arr0, @SuppressWarnings("unchecked") T[]... more)
  6. concat(T[] array1, T[] array2)
  7. concat(T[] array1, T[] array2)
  8. concat(T[] first, @SuppressWarnings("unchecked") T... second)
  9. concat(T[] first, T[] second)