Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Arrays;

public class Main {
    /**Concatenate two arrays of the same type and return it as an new array.
     *<p>
     *If the third parameter is big enough to store the return, it will be stored there; otherwise a new array will be created.
     *@param array1 One of the arrays to concatenate.
     *@param array2 The other one of the arrays to concatenate.
     *@param type The class of the copy to be returned.
     *@return The result of concatenating the array1 with the array2 
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] concatArrays(T[] array1, T[] array2, T[] type) {
        Object[] ret = new Object[array1.length + array2.length];
        System.arraycopy(array1, 0, ret, 0, array1.length);
        System.arraycopy(array2, 0, ret, array1.length, array2.length);
        return Arrays.asList(ret).toArray(type);
    }

    /**Concatenate many arrays of the same type and return it as an new array.
     *<p>
     *If the second parameter is big enough to store the return, it will be stored there; otherwise a new array will be created.
     *@param multiArray All the arrays that will be concatenated.
     *@param type The class of the copy to be returned.
     *@return The result of concatenating all the arrays in multiArray 
     */
    public static <T> T[] concatArrays(T[][] multiArray, T[] type) {
        if (multiArray.length == 0)
            return type;
        T[] ret = multiArray[0];
        for (int i = 1; i < multiArray.length; i++) {
            ret = concatArrays(ret, multiArray[i], type);
        }
        return ret;
    }
}