集册 Java实例教程 此方法允许合并2个数组

此方法允许合并2个数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

487
此方法允许合并2个数组


//package com.nowjava;

import java.lang.reflect.Array;
/* 
 来自 
*n o w j a v a . c o m - 时  代  Java*/


public class Main {

    /**

     * This method allows to merge 2 arrays

     *

     * @param <T> the generic type

     * @param firstArray the first array to merge

     * @param secondArray the second array to merge

     * @return merged array

     */

    public static <T> T[] mergeArrays(T[] firstArray, T[] secondArray) {


        int firstArrayLength = firstArray.length;

        int secondArrayLength = secondArray.length;


        @SuppressWarnings("unchecked")

        T[] mergedArray = (T[]) Array.newInstance(firstArray.getClass()

                .getComponentType(), firstArrayLength + secondArrayLength);

        System.arraycopy(firstArray, 0, mergedArray, 0, firstArrayLength);

        
展开阅读全文