Concatenate Java arrays : Array Insert Remove « Collections Data Structure « Java






Concatenate Java arrays

      
/*
 * Convenience methods for working with Java arrays.
 * Copyright (C) 2005 Stephen Ostermiller
 * http://ostermiller.org/contact.pl?regarding=Java+Utilities
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * See COPYING.TXT for details.
 */

import java.io.IOException;
import java.lang.reflect.Array;

/**
 * Convenience methods for working with Java arrays.
 * More information about this class is available from <a target="_top" href=
 * "http://ostermiller.org/utils/ArrayHelper.html">ostermiller.org</a>.
 *
 * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
 * @since ostermillerutils 1.06.00
 */
public final class ArrayHelper {

  /**
   * Concatenates the given arrays into a single long array.
   * The returned array will be of the common super type of the
   * two given arrays.
   * <p>
   * For example it can be called like this:<br>
   * <pre>
   * String[] arr = (String[])ArrayHelper.cat(new String[]{"one","two"}, new String[]{"three","four"});
   * </pre>
   *
   * @param arr1 first array
   * @param arr2 second array
   * @return an array whose length is the sum of the given array's lengths and contains all the elements in the given arrays.
   * @since ostermillerutils 1.06.00
   */
  public static Object[] cat(Object[] arr1, Object[] arr2){
    // Use reflection to find the super class of both arrays
    Class<?> commonSuperClass = Object.class;
    boolean foundcommonSuperClass=false;
    for (Class<?> c1 = arr1.getClass().getComponentType(); !foundcommonSuperClass && !c1.equals(Object.class); c1 = c1.getSuperclass()){
      for (Class<?> c2 = arr2.getClass().getComponentType(); !foundcommonSuperClass && !c2.equals(Object.class); c2 = c2.getSuperclass()){
        if (c2.equals(c1)){
          foundcommonSuperClass = true;
          commonSuperClass = c1;
        }
      }
    }
    // Create a new array of the correct type
    Object[] result = (Object[])Array.newInstance(commonSuperClass, arr1.length + arr2.length);
    // Copy the two arrays into the large array
    System.arraycopy(arr1, 0, result, 0, arr1.length);
    System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
    return result;
  }



  /**
   * Tests two arrays to see if the arrays are equal.
   * Two arrays will be equal only if they are the same length
   * and contain objects that are equal in the same order.
   *
   * @param arr1 first array
   * @param arr2 second array
   * @return true iff two arguments are equal
   * @since ostermillerutils 1.06.00
   */
  public static boolean equal(Object[] arr1, Object[] arr2){
    if (arr1 == null && arr2 == null) return true;
    if (arr1 == null || arr2 == null) return false;
    if (arr1.length != arr2.length) return false;
    for (int i=0; i<arr1.length; i++){
      if (!equalObjects(arr1[i], arr2[i])) return false;
    }
    return true;
  }

  /**
   * Tests if the two objects are equal
   *
   * @param o1 first object
   * @param o2 second object
   * @since ostermillerutils 1.06.00
   */
  private static boolean equalObjects(Object o1, Object o2){
    if (o1 == null && o2 == null) return true;
    if (o1 == null || o2 == null) return false;
    return o1.equals(o2);
  }
}

   
    
    
    
    
    
  








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.This program demonstrates array manipulation.
34.Appends an Object to an Object array.
35.Appends an integer to an integer array.
36.Inserts an Object into an Object array at the index position.
37.Prepends an Object to an Object array.
38.Removes an Object from an Object array.
39.Removes duplicate elements from the array
40.Creates a new subarray from a larger array.
41.Append one array to another
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.