集册 Java实例教程 将传递的新值添加到传递的数组的末尾。

将传递的新值添加到传递的数组的末尾。

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

399
将传递的新值添加到传递的数组的末尾。

/**

 * Helios, OpenSource Monitoring

 * Brought to you by the Helios Development Group

 *

 * Copyright 2007, Helios Development Group and individual contributors

 * as indicated by the @author tags. See the copyright.txt file in the

 * distribution for a full listing of individual contributors.

 *

 * This is free software; you can redistribute it and/or modify it

 * under the terms of the GNU Lesser General Public License as

 * published by the Free Software Foundation; either version 2.1 of

 * the License, or (at your option) any later version.

 *

 * This software 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

 * Lesser General Public License for more details.

 *

 * You should have received a copy of the GNU Lesser General Public

 * License along with this software; if not, write to the Free

 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA

 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 

 *

 */

//package com.nowjava;/**from 时   代     Java  公  众  号 - nowjava.com**/


import java.lang.reflect.Array;


public class Main {

    /**

     * Adds the passed new values to the end of the passed array.

     * Null values are dropped from both arrays.

     * @param array The prefix array.

     * @param newValues The array to be appended.

     * @return A newly sized or created array.

     */

    public static <E> E[] append(E[] array, E... newValues) {

        return append(true, array, newValues);

    }


    /**

     * Adds the passed new values to the end of the passed array.

     * @param array The prefix array.

     * @param dropNulls Indicates if null array elements from either should be dropped.

     * @param newValues The array to be appended.

     * @return A newly sized or created array.

     */

    @SuppressWarnings("unchecked")

    public static <E> E[] append(boolean dropNulls, E[] array,

            E... newValues) {

        try {

            if (newValues == null || newValues.length < 1)

                return (E[]) (dropNulls ? Array.newInstance(array

                        .getClass().getComponentType(), 0) : array);

            if (array == null) {/* 来自 时 代 J a v a - N o w J a v a . c o m*/

                if (newValues != null) {

                    Class clazz = newValues.getClass();

                    Class arrType = clazz.getComponentType();

                    array = (E[]) Array.newInstance(arrType, 0);

                } else {

                    return array;

                }

            }

            int nullItemCount = 0;

            if (dropNulls) {

                for (E e : array) {

                    if (e == null)

                        nullItemCount++;

                }

                for (E e : newValues) {

                    if (e == null)

                        nullItemCount++;

                }

            }

            int newSize = array == null ? newValues.length : array.length

                    + newValues.length - nullItemCount;

            E[] newArray = (E[]) Array.newInstance(newValues.getClass()

                    .getComponentType(), newSize);

            int offset = 0;

            if (!dropNulls) {

                if (!(array == null && array.length < 1)) {

                    System.arraycopy(array, 0, newArray, 0, array.length);

                    offset = array.length;

                }

                System.arraycopy(newValues, 0, newArray, offset,

                        newValues.length);

            } else {

                
展开阅读全文