Appends an element to a copy of the array and returns the copy. - Android java.lang

Android examples for java.lang:array copy

Description

Appends an element to a copy of the array and returns the copy.

Demo Code

import java.lang.reflect.Array;

public class Main{

    /**//from   w w w .j  av  a  2 s. c  o  m
     * Appends an element to a copy of the array and returns the copy.
     * @param array The original array, or null to represent an empty array.
     * @param element The element to add.
     * @return A new array that contains all of the elements of the original array
     * with the specified element added at the end.
     */
    @SuppressWarnings("unchecked")
    public static <T> T[] appendElement(Class<T> kind, T[] array, T element) {
        final T[] result;
        final int end;
        if (array != null) {
            end = array.length;
            result = (T[]) Array.newInstance(kind, end + 1);
            System.arraycopy(array, 0, result, 0, end);
        } else {
            end = 0;
            result = (T[]) Array.newInstance(kind, 1);
        }
        result[end] = element;
        return result;
    }

}

Related Tutorials