Java tutorial
//package com.java2s; /************************************************************************************** * Copyright (C) 2008 EsperTech, Inc. All rights reserved. * * http://esper.codehaus.org * * http://www.espertech.com * * ---------------------------------------------------------------------------------- * * The software in this package is published under the terms of the GPL license * * a copy of which has been included with this distribution in the license.txt file. * **************************************************************************************/ import java.lang.reflect.Array; import java.util.*; public class Main { public static Object arrayExpandAddElements(Object array, Object[] elementsToAdd) { Class cl = array.getClass(); if (!cl.isArray()) return null; int length = Array.getLength(array); int newLength = length + elementsToAdd.length; Class componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(array, 0, newArray, 0, length); for (int i = 0; i < elementsToAdd.length; i++) { Array.set(newArray, length + i, elementsToAdd[i]); } return newArray; } public static Object arrayExpandAddElements(Object array, Collection elementsToAdd) { Class cl = array.getClass(); if (!cl.isArray()) return null; int length = Array.getLength(array); int newLength = length + elementsToAdd.size(); Class componentType = array.getClass().getComponentType(); Object newArray = Array.newInstance(componentType, newLength); System.arraycopy(array, 0, newArray, 0, length); int count = 0; for (Object element : elementsToAdd) { Array.set(newArray, length + count, element); count++; } return newArray; } }