集册 Java实例教程 它接受一个数组并将指定元素添加到它的末尾

它接受一个数组并将指定元素添加到它的末尾

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

361
它需要一个Array并将指定的元素添加到它的末尾
//来自 n  o  w  j  a  v  a . c o m


//package com.nowjava;

import java.lang.reflect.Array;


public class Main {

    /**

     * It takes an Array and add the specified element to the end of the it

     *

     * @param <T> Type

     * @param typeClass the class of the type of the array

     * @param array the array you want to operate on

     * @param element the element you want to add

     * @return the new extended array

     */

    public static <T> T[] addElement(Class<T> typeClass, T[] array,

            T element) {

        @SuppressWarnings("unchecked")

        T[] newArray = (T[]) Array.newInstance(typeClass, array.length + 1);

        int i = 0;

        while (i < array.length) {

            newArray[i] = array[i];
展开阅读全文