Append one array to another : Array Insert Remove « Collections Data Structure « Java






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;
  }

}

   
    
    
    
    
  








Related examples in the same category

1.Adds all the elements of the given arrays into a new boolean-value array.
2.Adds all the elements of the given arrays into a new byte-type array.
3.Adds all the elements of the given arrays into a new char-type array.
4.Adds all the elements of the given arrays into a new double-type array.
5.Adds all the elements of the given arrays into a new float-type array.
6.Adds all the elements of the given arrays into a new int-type array.
7.Adds all the elements of the given arrays into a new long-type array.
8.Adds all the elements of the given arrays into a new short-type array.
9.Copies the given array and adds the given element at the end of the new array. (char type value)
10.Copies the given array and adds the given element at the end of the new array. (float type value)
11.Copies the given array and adds the given element at the end of the new array. (long value type)
12.Copies the given array and adds the given element at the end of the new array. (object value type)
13.Copies the given array and adds the given element at the end of the new array.(boolean value type)
14.Copies the given array and adds the given element at the end of the new array.(byte value type)
15.Copies the given array and adds the given element at the end of the new array.(double type value)
16.Copies the given array and adds the given element at the end of the new array.(int value type)
17.Copies the given array and adds the given element at the end of the new array.(short type array)
18.Inserts the specified element at the specified position in the array.
19.Inserts the specified element at the specified position in the boolean-type-value array.
20.Inserts the specified element at the specified position in the byte-type-value array.
21.Inserts the specified element at the specified position in the char-type-value array.
22.Inserts the specified element at the specified position in the double-type-value array.
23.Inserts the specified element at the specified position in the float-value-type array.
24.Inserts the specified element at the specified position in the int-type-value array.
25.Inserts the specified element at the specified position in the long-type-value array.
26.Inserts the specified element at the specified position in the short-value-type array.
27.Removes the element at the specified position from the specified array.
28.Removes the element at the specified position from the specified long type array.
29.Removes the first occurrence of the specified element from the specified array.
30.Removes the first occurrence of the specified element from the specified long value array.
31.Insert value to array
32.Array Copy Utilities
33.Concatenate Java arrays
34.This program demonstrates array manipulation.
35.Appends an Object to an Object array.
36.Appends an integer to an integer array.
37.Inserts an Object into an Object array at the index position.
38.Prepends an Object to an Object array.
39.Removes an Object from an Object array.
40.Removes duplicate elements from the array
41.Creates a new subarray from a larger array.
42.Array Expander
43.Array Util: seach, insert, append, remove, copy, shuffle
44.Add new value to exsisting array.The new value is indexed to the last.
45.Add new value, which sets as the first value, to existing array.
46.Add one array to another
47.Concatenates all the passed arrays
48.Returns a copy of the specified array of objects of the specified size.
49.Pack chunks
50.Removes an element from a an array yielding a new array with that data.