org.gjt.rec.util
Class Arrayz

java.lang.Object
  |
  +--org.gjt.rec.util.Arrayz

public class Arrayz
extends Object

A toolbox full of tools for manupulating arrays.

Author:
Neil Stockbridge
See Also:
Arrays

Field Summary
static String REVISION
           
 
Method Summary
static Object add(Object array, Object element)
          Appends a partciular element to a particular array.
static boolean contains(Object array, Object element)
          Checks for the presence of a particular element in a particular array.
static boolean equals(Object firstArray, Object secondArray)
          Compares two arrays to see if they are equal.
static void fill(Object array, Object value)
          Fills an array with a particular value.
static int indexOf(Object array, Object element)
          Scans a particular array for the presence of a particular element.
static boolean isEmpty(Object array)
          Determines if there are any elements in a particular array.
static int lastIndexOf(Object array, Object element)
          Scans a particular array for the presence of a particular element.
static Object remove(Object array, int index)
          Removes the element at a particular index from a particular array.
static int size(Object array)
          Finds the number of elements in a particular array.
static Object subList(Object array, int fromIndex, int toIndex)
          Creates a new array containing all elements in a particular array from a particular index up to but not including a particular end index.
static String toString(Object array)
          Invoking toString on an array doesn't tell you anything about what is in the array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REVISION

public static final String REVISION
See Also:
Constant Field Values
Method Detail

size

public static int size(Object array)
Finds the number of elements in a particular array.

Parameters:
array - the array in which to count the elements.
Returns:
the number of elements in the supplied array.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array.

isEmpty

public static boolean isEmpty(Object array)
Determines if there are any elements in a particular array.

Parameters:
array - the array in which to count the elements.
Returns:
true if there is at least one element in the supplied array.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array.

contains

public static boolean contains(Object array,
                               Object element)
Checks for the presence of a particular element in a particular array. Note that this method will return true if the supplied element is null and there is an element in the array which is also null or if the supplied element is not null, it will return true if there is an element in the array, to which the supplied element is equal according to .equals(Object).

Parameters:
array - the array in which to look for the element.
element - the element to try to find in the array.
Returns:
true if the supplied element was present.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array.

indexOf

public static int indexOf(Object array,
                          Object element)
Scans a particular array for the presence of a particular element. If the element is found, this method will return the first index at which it was found. An element is "found" in the array if either the supplied element is null and there is an element in the array which is also null or if the supplied element is not null, if there is an element in the array, to which the supplied element is equal according to .equals(Object).

Parameters:
array - the array in which to look for the element.
element - the element to try to find in the array.
Returns:
the first index at which the element was present or -1 if no such element was found.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array.

lastIndexOf

public static int lastIndexOf(Object array,
                              Object element)
Scans a particular array for the presence of a particular element. If the element is found, this method will return the last index at which it was found. An element is "found" in the array if either the supplied element is null and there is an element in the array which is also null or if the supplied element is not null, if there is an element in the array, to which the supplied element is equal according to .equals(Object).

Parameters:
array - the array in which to look for the element.
element - the element to try to find in the array.
Returns:
the last index at which the element was present or -1 if no such element was found.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array.

subList

public static Object subList(Object array,
                             int fromIndex,
                             int toIndex)
Creates a new array containing all elements in a particular array from a particular index up to but not including a particular end index.

Parameters:
array - the array from which to take the elements.
fromIndex - the index of the first element to copy in to the new array.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array.
ArrayIndexOutOfBoundsException - if toIndex < fromIndex or if fromIndex < 0 or if the array size < toIndex.

add

public static Object add(Object array,
                         Object element)
Appends a partciular element to a particular array.

Parameters:
array - the array to append the element to. if this argument is null then a new array will be created.
Returns:
the new array holding all the elements of the original one plus the appended element.
Throws:
IllegalArgumentException - if the object supplied for the first argument is not an array or if the element to append is the wrong type for the array.

remove

public static Object remove(Object array,
                            int index)
Removes the element at a particular index from a particular array.

Parameters:
array - the array to remove the element from.
index - the index of the element to remove.
Returns:
the new array holding all the elements of the original except the element that was removed.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array or is null.
ArrayIndexOutOfBoundsException - if the supplied index is out of range for the supplied array.

equals

public static boolean equals(Object firstArray,
                             Object secondArray)
Compares two arrays to see if they are equal. Two arrays will be equal if They are both null OR They both have the same component type AND they are the same length AND for any index i, the elements at index i in the two arrays are either both null or both equal according to .equals(Object).

Parameters:
firstArray - the first of the two arrays to compare or null.
secondArray - the second of the two arrays to compare or null.
Returns:
true if the two arrays are equal.
Throws:
IllegalArgumentException - if either of the supplied arguments are not arrays.

toString

public static String toString(Object array)
Invoking toString on an array doesn't tell you anything about what is in the array. Using Arrayz.toString(array) instead, results in far more information.

Parameters:
array - the array to work out a toString() for.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array.

fill

public static void fill(Object array,
                        Object value)
Fills an array with a particular value. Even works for multi- dimensional arrays.

Parameters:
array - the array to fill.
value - the value to fill the array with.
Throws:
IllegalArgumentException - if the object supplied as an array is not an array.
NullPointerException - if array is null