集册 Java实例教程 要进行数组合并的泛型方法

要进行数组合并的泛型方法

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

466
要进行数组合并的泛型方法

/*

 * oxCore is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.

 *

 * Copyright (c) 2014, Gluu

 */

//package com.nowjava;
/**
来 自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m
**/

import java.lang.reflect.Array;


public class Main {

    @SuppressWarnings("unchecked")

    public static <T> T[] arrayMerge(T[]... arrays) {

        // Determine required size of new array

        int count = 0;

        for (T[] array : arrays) {

            count += array.length;

        }


        if (count == 0) {

            return (T[]) Array.newInstance(arrays.getClass()

                    .getComponentType().getComponentType(), 0);

        }


        // create new array of required class/**来 自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m**/

        T[] mergedArray = (T[]) Array.newInstance(arrays[0][0].getClass(),

                count);


        // Merge each array into new array

        int start = 0;

     
展开阅读全文