org.jscience.architecture.traffic.util
Class ArrayUtils

java.lang.Object
  extended by org.jscience.architecture.traffic.util.ArrayUtils

public class ArrayUtils
extends java.lang.Object

These functions can be used to add and remove elements from an array.


Constructor Summary
ArrayUtils()
           
 
Method Summary
static java.lang.Object addArray(java.lang.Object ar1, java.lang.Object ar2)
          Creates a new array containing the elements of two arrays.
static java.lang.Object addArrayUnique(java.lang.Object ar1, java.lang.Object ar2)
          Creates a new array containing the unique elements of two arrays.
static java.lang.Object addElement(java.lang.Object ar, java.lang.Object elem)
          Adds an element to the end of an array.
static java.lang.Object addElementUnique(java.lang.Object ar, java.lang.Object elem)
          Adds an element to the end of an array, if and only if the element is not already in the array.
static java.lang.Object adjustArraySize(java.lang.Object[] input, int newLength, java.lang.Class newClass)
          Change the size of an array.
static java.lang.Object concatArray(java.lang.Object ar)
          Concatenates a two dimensional array to a one dimensional
static java.lang.Object createArray(java.lang.String arrayName, int length)
          Yet another way to create an array
static java.lang.Object createArray(java.lang.String className, int length, int dimension)
          Creates a n-dimensional array of a certain class
static java.lang.Object createSingleArray(java.lang.String className, int length)
          Creates a one-dimensional array of a certain class.
static java.lang.Object cropArray(java.lang.Object ar, int length)
          Crops an array to specified length.
static int findElement(java.lang.Object ar, java.lang.Object elem)
          Checks if an element is part of an array, using the == operator.
static int findElementA(java.lang.Object[] ar, java.lang.Object elem)
          DOCUMENT ME!
static java.lang.String getAtomaryComponent(java.lang.String className)
          Calculates the atomary component of an array by the name of its class.
static java.lang.String getComponentClassName(java.lang.String className)
          Calculates the direct component of an array by the name of its class.
static int getDimensionClassName(java.lang.String className)
          Determines the dimension of an array by the name of its class
static java.util.Enumeration getEmptyEnumeration()
          Returns an empty Enumeration
static java.util.Enumeration getEnumeration(java.lang.Object[] ar)
          Returns an Enumeration over the given array
static java.util.Enumeration getEnumeration(java.lang.Object[][] ar)
          Returns an Enumeration over the given 2d array
static java.lang.Object[] randomizeArray(java.lang.Object[] ar)
          Replaces the elements of the provided array in a random fashion.
static int[] randomizeIntArray(int[] arr, java.util.Random r)
           
static java.lang.Object remElement(java.lang.Object ar, int pos)
          Removes the element at given position, and moves all elements above that 1 position down.
static java.lang.Object remElement(java.lang.Object ar, java.lang.Object elem)
          Removes an element from an array.
static java.lang.Object resizeArray(java.lang.Object[] ar, int length)
          Resizes an array to specified length.
static java.lang.Object setElement(java.lang.Object ar, java.lang.Object oldo, java.lang.Object newo)
          Replaces an element in an array with another.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayUtils

public ArrayUtils()
Method Detail

addElement

public static java.lang.Object addElement(java.lang.Object ar,
                                          java.lang.Object elem)
Adds an element to the end of an array. Although the return type is an Object, you can cast it to an array of the same type as ar.

Parameters:
ar - The array to add an element to.
elem - The element to add to the end of the array.
Returns:
A new array with the same elements as ar, with elem at the end.

addElementUnique

public static java.lang.Object addElementUnique(java.lang.Object ar,
                                                java.lang.Object elem)
Adds an element to the end of an array, if and only if the element is not already in the array. Comparison is done using the == operator, NOT the Object.equals method. Although the return type is an Object, you can cast it to an array of the same type as ar.

Parameters:
ar - The array to add an element to.
elem - The element to add to the end of the array.
Returns:
A new array with the same elements as ar, with elem at the end.

addArray

public static java.lang.Object addArray(java.lang.Object ar1,
                                        java.lang.Object ar2)
Creates a new array containing the elements of two arrays. The elements of the first array will be located at the front of the resulting array, while the elements of the second will appear at the end. This order is guaranteed. Although the return type is an Object, you can cast it to an array of the same type as the first array.

Parameters:
ar1 - The array to add the other array to.
ar2 - The array to add at the end of the first array.
Returns:
A new array with the same elements as ar, with all elements of arrtoadd added at the end.

addArrayUnique

public static java.lang.Object addArrayUnique(java.lang.Object ar1,
                                              java.lang.Object ar2)
Creates a new array containing the unique elements of two arrays. Comparison is done using the == operator, NOT Object.equals. This means the null element will appear once in the result, iff it appears at least once in either of the two input arrays. The order of the elements is the same as the two input arrays, with the elements of the first array up front. Although the return type is an Object, you can cast it to an array of the same type as the first (or second for that matter) array.

Parameters:
ar1 - The array to add the other array to.
ar2 - The array to add at the end of the first array.
Returns:
A new array with the unique elements of the first and second array.

remElement

public static java.lang.Object remElement(java.lang.Object ar,
                                          int pos)
Removes the element at given position, and moves all elements above that 1 position down. Although the return type is an Object[], you can cast it to an array of the same type as ar.

Parameters:
ar - The array to remove the element from.
pos - The position of the element to remove.
Returns:
A new array containing all elements of ar, except the element at position pos.

remElement

public static java.lang.Object remElement(java.lang.Object ar,
                                          java.lang.Object elem)
Removes an element from an array. Not an equals method is used, but a direct == comparison. This method will thus only remove the exact same element from the array. Although the return type is an Object[], you can cast it to an array of the same type as ar.

Parameters:
ar - The array to remove the element from.
elem - The element to remove.
Returns:
A new array containing all elements of ar, except elem.

setElement

public static java.lang.Object setElement(java.lang.Object ar,
                                          java.lang.Object oldo,
                                          java.lang.Object newo)
Replaces an element in an array with another. Although the return type is an Object[], you can cast it to an array of the same type as ar.

Parameters:
ar - The array to replace the element in.
oldo - The element to replace.
newo - The element to replace it with.
Returns:
A new array containing all elements of ar, except elem.

findElement

public static int findElement(java.lang.Object ar,
                              java.lang.Object elem)
Checks if an element is part of an array, using the == operator.

Parameters:
ar - The array to search.
elem - The element to search for.
Returns:
The index of the element in the array. If the element is not in the array, returns -1.

findElementA

public static int findElementA(java.lang.Object[] ar,
                               java.lang.Object elem)
DOCUMENT ME!

Parameters:
ar - DOCUMENT ME!
elem - DOCUMENT ME!
Returns:
DOCUMENT ME!

randomizeIntArray

public static int[] randomizeIntArray(int[] arr,
                                      java.util.Random r)

randomizeArray

public static java.lang.Object[] randomizeArray(java.lang.Object[] ar)
Replaces the elements of the provided array in a random fashion.

Parameters:
ar - The array to randomize.
Returns:
The array in which the elements are replaced randomly..

adjustArraySize

public static java.lang.Object adjustArraySize(java.lang.Object[] input,
                                               int newLength,
                                               java.lang.Class newClass)
                                        throws java.lang.ClassNotFoundException,
                                               java.lang.InstantiationException,
                                               java.lang.IllegalAccessException
Change the size of an array. If the size of the array increases, then fill the new elements of the array (at the end) with new instances of a specific class.

Parameters:
input - The original array
newLength - The new length of the array
newClass - The class of the new elements. It is necessary that objects of this class can be instantiated with Class.newInstance. If the new length is less than the old length of the array, then this parameter is irrelevant.
Returns:
The result array
Throws:
java.lang.ClassNotFoundException - If this method wants to create objects, but it cannot find the class that newClass is referring to.
java.lang.InstantiationException - If this method wants to create objects, but something goes wrong.
java.lang.IllegalAccessException - DOCUMENT ME!

cropArray

public static java.lang.Object cropArray(java.lang.Object ar,
                                         int length)
Crops an array to specified length. Removes all elements after the specified length.

Parameters:
ar - The array to crop.
length - The length to crop to.
Returns:
A new array with the same elements as ar, cropped to length len

resizeArray

public static java.lang.Object resizeArray(java.lang.Object[] ar,
                                           int length)
Resizes an array to specified length.

Parameters:
ar - The array to resize.
length - The length to resize to.
Returns:
A new array with the same elements as ar, resized to length len

concatArray

public static java.lang.Object concatArray(java.lang.Object ar)
Concatenates a two dimensional array to a one dimensional

Parameters:
ar - Two dimensional array to concatenate
Returns:
A new array with all the elements of the original array in its second dimension

getDimensionClassName

public static int getDimensionClassName(java.lang.String className)
Determines the dimension of an array by the name of its class

Parameters:
className - classname of an array.
Returns:
Its dimension

getComponentClassName

public static java.lang.String getComponentClassName(java.lang.String className)
Calculates the direct component of an array by the name of its class. So for instance : the direct component of "[[[I" is "[[I".

Parameters:
className - classname of an array
Returns:
The classname of its direct component. If the direct component is a "normal class" then the classname is converted to the standard notation. Names of primitive types are NOT converted (see java.lang.Class documentation for their meanings).

getAtomaryComponent

public static java.lang.String getAtomaryComponent(java.lang.String className)
Calculates the atomary component of an array by the name of its class. So for instance : the atomary component of "[[[I" is "I".

Parameters:
className - classname of an array
Returns:
The classname of its atomary component. If the direct component is a "normal class" then the classname is converted to the standard notation. Names of primitive types are NOT converted (see java.lang.Class documentation for their meanings).

createSingleArray

public static java.lang.Object createSingleArray(java.lang.String className,
                                                 int length)
                                          throws java.lang.ClassNotFoundException
Creates a one-dimensional array of a certain class.

Parameters:
className - The classname of the components of this array. This method accepts normal class names and the one letter designations for primitive types as used in java.lang.Class.
length - The length of the array. Positive int of course.
Returns:
The resulting array
Throws:
java.lang.ClassNotFoundException - It's all in the name

createArray

public static java.lang.Object createArray(java.lang.String className,
                                           int length,
                                           int dimension)
                                    throws java.lang.ClassNotFoundException
Creates a n-dimensional array of a certain class

Parameters:
className - The classname of the components of this array. This method accepts normal class names and the one letter designations for primitive types as used in java.lang.Class.
length - The length of the array. Positive int of course.
dimension - The dimension of this array
Returns:
The resulting array.
Throws:
java.lang.ClassNotFoundException - It's all in the name

createArray

public static java.lang.Object createArray(java.lang.String arrayName,
                                           int length)
                                    throws java.lang.ClassNotFoundException
Yet another way to create an array

Parameters:
arrayName - The classname of the array that you want to create
length - The desired length
Returns:
The resulting array.
Throws:
java.lang.ClassNotFoundException - It's all in the name

getEnumeration

public static java.util.Enumeration getEnumeration(java.lang.Object[] ar)
Returns an Enumeration over the given array

Parameters:
ar - DOCUMENT ME!
Returns:
DOCUMENT ME!

getEnumeration

public static java.util.Enumeration getEnumeration(java.lang.Object[][] ar)
Returns an Enumeration over the given 2d array

Parameters:
ar - DOCUMENT ME!
Returns:
DOCUMENT ME!

getEmptyEnumeration

public static java.util.Enumeration getEmptyEnumeration()
Returns an empty Enumeration

Returns:
DOCUMENT ME!
Throws:
java.util.NoSuchElementException - DOCUMENT ME!