Package com.sun.javatest.util
Class DynamicArray
java.lang.Object
com.sun.javatest.util.DynamicArray
A class that manipulates arrays of Objects (no primitives).
Currently, you can remove any element and have the array shrink itself,
and you can append an element and have it expand.
Note: You should either initialize array variables to a zero length array
or use the append method that accepts the Class argument to prevent array
storage exceptions. This is an issue when this class is forced to create a
new array of unknown type and determine the type from the item being appended.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]
append
(T[] oldArr, T newObj) Append an object to the end of the array.static <T> T[]
Append an object to the end of the array.static <T> int
find
(T[] arr, T target) Find the first index of the object that is equal (reference) to the target.protected static Class
<?> getArrayClass
(Object... arr) Get the type of objects that the array is declared to hold.static <T> T[]
insert
(T[] oldArr, T newObj, int location) Insert an object into the array at the specified index.static <T> T[]
join
(T[] array1, T... array2) Join two arrays.static <T> T[]
remove
(T[] oldArr, int index) Remove the object at the specified index.static <T> T[]
remove
(T[] oldArr, T victim) Remove the object from the array.
-
Constructor Details
-
DynamicArray
public DynamicArray()
-
-
Method Details
-
append
public static <T> T[] append(T[] oldArr, T newObj) Append an object to the end of the array.- Parameters:
oldArr
- The original array which we are appending to. May be zero length or null.newObj
- The new value to append to the oldArr. Null is not a legal value.- Returns:
- A new array with the object appended to it.
- Throws:
IllegalArgumentException
- If newObj is null.ArrayStoreException
- If there is a type mismatch between oldArr and newObj.
-
append
Append an object to the end of the array.- Parameters:
oldArr
- The original array which we are appending to. May be zero length or null.newObj
- The new value to append to the oldArr.arrayClass
- If the oldArr is null, a new array of arrayClass will be created. If arrayClass is null, the type of newObj will be used to create the array.- Returns:
- A new array with the object appended to it.
- Throws:
IllegalArgumentException
- If newObj is null.ArrayStoreException
- If there is a type mismatch between oldArr and newObj.
-
join
public static <T> T[] join(T[] array1, T... array2) Join two arrays.- Parameters:
array1
- The first array to be joined.array2
- The second array to be joined.- Returns:
- If either argument is null, the result is the other argument; otherwise, the result is a new array, whose element type is taken from the first array, containing the elements of the first array, followed by the elements of the second array.
-
insert
public static <T> T[] insert(T[] oldArr, T newObj, int location) Insert an object into the array at the specified index.- Parameters:
oldArr
- The array to insert into. May be null, in which case a new array will be created using the type ofnewObj
.newObj
- The object to insert.location
- The index at which to insert the item. An exception will occur if the location in out of range. This location can be equal tooldArr.length
, in which case this becomes an append operation.- Returns:
- A new array with the object inserted into it at the specified location.
-
remove
public static <T> T[] remove(T[] oldArr, int index) Remove the object at the specified index. The new array type is determined by the type of element 0 in the original array.- Parameters:
oldArr
- The original array which we are removing from. May not be null or zero length.index
- The index to remove fromoldArr
. Exception will be thrown if it is out of range.- Returns:
- An array of the same type as the original array (element 0), without the given index. Zero length array if the last element is removed.
- Throws:
IllegalArgumentException
- Will occur if the given index is out of range, or the given array is null.NullPointerException
- May occur if the source array has null values, particularly in the first array position.ArrayStoreException
- May occur if all the objects in the array do not match.
-
remove
public static <T> T[] remove(T[] oldArr, T victim) Remove the object from the array. If the object is not found, the original array is returned unchanged. The first instance of the target is removed.- Parameters:
oldArr
- The array to remove from.victim
- The object to remove from the array. May be null.- Returns:
- Zero length array if the last element is removed.
-
find
public static <T> int find(T[] arr, T target) Find the first index of the object that is equal (reference) to the target.- Parameters:
arr
- The data to search; should not be null.target
- The reference to search for; can be null.- Returns:
- The array index of the target.
-
getArrayClass
Get the type of objects that the array is declared to hold.- Parameters:
arr
- The array to examine.- Returns:
- The class of objects that the given array can hold.
-