Mega Code Archive

 
Categories / Java / Collections Data Structure
 

Append one array to another

/* Copyright (C) 2003 Univ. of Massachusetts Amherst, Computer Science Dept.    This file is part of "MALLET" (MAchine Learning for LanguagE Toolkit).    http://www.cs.umass.edu/~mccallum/mallet    This software is provided under the terms of the Common Public License,    version 1.0, as published by http://www.opensource.org.  For further    information, see the file `LICENSE' included with this distribution. */ //package cc.mallet.util; import java.lang.reflect.Array; /**  * Static utility methods for arrays (like java.util.Arrays, but more useful).  *   * @author <a href="mailto:casutton@cs.umass.edu">Charles Sutton</a>  * @version $Id: ArrayUtils.java,v 1.1 2007/10/22 21:37:40 mccallum Exp $  */ public class Util {   /**    * Returns a new array that is the concatenation of a1 and a2.    *     * @param a1    * @param a2    * @return    */   public static int[] append(int[] a1, int[] a2) {     int[] ret = new int[a1.length + a2.length];     System.arraycopy(a1, 0, ret, 0, a1.length);     System.arraycopy(a2, 0, ret, a1.length, a2.length);     return ret;   }   /**    * Returns a new array that is the concatenation of a1 and a2.    *     * @param a1    * @param a2    * @return    */   public static double[] append(double[] a1, double[] a2) {     double[] ret = new double[a1.length + a2.length];     System.arraycopy(a1, 0, ret, 0, a1.length);     System.arraycopy(a2, 0, ret, a1.length, a2.length);     return ret;   }   /**    * Returns a new array with a single element appended at the end. Use this    * sparingly, for it will allocate a new array. You can easily turn a    * linear-time algorithm to quadratic this way.    *     * @param v    *            Original array    * @param elem    *            Element to add to end    */   public static int[] append(int[] v, int elem) {     int[] ret = new int[v.length + 1];     System.arraycopy(v, 0, ret, 0, v.length);     ret[v.length] = elem;     return ret;   }   /**    * Returns a new array with a single element appended at the end. Use this    * sparingly, for it will allocate a new array. You can easily turn a    * linear-time algorithm to quadratic this way.    *     * @param v    *            Original array    * @param elem    *            Element to add to end    */   public static boolean[] append(boolean[] v, boolean elem) {     boolean[] ret = new boolean[v.length + 1];     System.arraycopy(v, 0, ret, 0, v.length);     ret[v.length] = elem;     return ret;   }   /**    * Returns a new array with a single element appended at the end. Use this    * sparingly, for it will allocate a new array. You can easily turn a    * linear-time algorithm to quadratic this way.    *     * @param v    *            Original array    * @param elem    *            Element to add to end    * @return Array with length v+1 that is (v0,v1,...,vn,elem). Runtime type    *         will be same as he pased-in array.    */   public static Object[] append(Object[] v, Object elem) {     Object[] ret = (Object[]) Array.newInstance(v.getClass()         .getComponentType(), v.length + 1);     System.arraycopy(v, 0, ret, 0, v.length);     ret[v.length] = elem;     return ret;   } }