Adds the passed new values to the end of the passed array. - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

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

Demo Code

/**/*w w  w . j  a  va2 s . com*/
 * 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.java2s;

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) {
                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 {
                int index = 0;
                for (E e : array) {
                    if (e != null) {
                        newArray[index] = e;
                        index++;
                    }
                }
                for (E e : newValues) {
                    if (e != null) {
                        newArray[index] = e;
                        index++;
                    }
                }
            }
            return newArray;
        } catch (Exception e) {
            throw new RuntimeException("Failed to append", e);
        }
    }
}

Related Tutorials