/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * History
 * Created on Oct 28, 2004 by kofke, schultz
 */
package etomica.util;

import static java.util.Arrays.copyOf;

/**
 * Non-instantiable class with static utility methods for working with arrays.
 */
public final class Arrays {

	/**
	 * Private constructor to prevent instantiation.
	 */
	private Arrays() {	}

    /**
     * Returns an array formed from adding the newObject to the elements
     * in the given array.  New array is one element larger than given array,
     * and is of same type as given array.  newObject is placed at the end of 
     * the new array.
     * @param objects array with objects to be put in new array
     * @param newObject object placed at end of new array
     * @return new array 
     */
	public static Object[] addObject(Object[] objects, Object newObject) {
		objects = copyOf(objects,objects.length+1);
		objects[objects.length-1] = newObject;
		return objects;
	}

    /**
     * Returns an array formed by removing the given object from the given array.  
     * New array is one element smaller than given array,
     * and is of same type as given array.  Order of objects in the new
     * array is unchanged from their order in the original array. 
     * If object is not in the given array,
     * method returns original array without performing any action.
     * @param array array with the objects
     * @param object object being removed
     * @return new array with the object removed
     */
	public static Object[] removeObject(Object[] array, Object object) {
		int length = array.length;
		for (int i=0; i<length; i++) {
			if (array[i] == object) {//look for object in array
				Object lastObject = array[length-1];//save last object, which is about to be dropped
				array = copyOf(array,length-1);//shorten array, dropping last object
				if (i < length-2) {//overwrite target object
					System.arraycopy(array,i+1,array,i,length-i-2);
				}
				if (i < length-1) {//recover last object
					array[length-2] = lastObject;
				}
				break;
			}
		}
		return array;
	}

}

@@ -930,17 +930,16 @@
 ew array
- 
 %0A     */
@@ -954,22 +954,21 @@
  static 
-Object
+%3CT%3E T
 %5B%5D addOb
@@ -972,22 +972,17 @@
 dObject(
-Object
+T
 %5B%5D objec
@@ -985,22 +985,17 @@
 bjects, 
-Object
+T
  newObje
@@ -1667,22 +1667,21 @@
  static 
-Object
+%3CT%3E T
 %5B%5D remov
@@ -1688,22 +1688,17 @@
 eObject(
-Object
+T
 %5B%5D array
@@ -1699,22 +1699,17 @@
  array, 
-Object
+T
  object)
@@ -1832,22 +1832,17 @@
 ray%0A%09%09%09%09
-Object
+T
  lastObj

Change Arrays add and remove object methods to use type parameter