Java examples for Collection Framework:Array Length
resize array and append array element
//package com.book2s; import java.lang.reflect.Array; public class Main { public static <T> T[] append(T[] buffer, T newElement) { T[] t = resize(buffer, buffer.length + 1, newElement.getClass()); t[buffer.length] = newElement;/*from w w w . java 2 s . co m*/ return t; } public static <T> T[] resize(T[] buffer, int newSize, Class<?> componentType) { T[] newArray = newArray(componentType, newSize); System.arraycopy(buffer, 0, newArray, 0, buffer.length >= newSize ? newSize : buffer.length); return newArray; } public static <T> T[] resize(T[] buffer, int newSize) { return resize(buffer, newSize, buffer.getClass().getComponentType()); } @SuppressWarnings("unchecked") public static <T> T[] newArray(Class<?> componentType, int newSize) { return (T[]) Array.newInstance(componentType, newSize); } }