Package com.xmlmind.util
Class ArrayUtil
- java.lang.Object
-
- com.xmlmind.util.ArrayUtil
-
public final class ArrayUtil extends Object
A collection of utility functions (static methods) operating on arrays. Complements what's found in java.util.Arrays.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T[]append(T[] list, T item)Inserts specified item in specified array at the end of specified array.static <T> intfind(T[] list, T item)Searches specified array for specified item.static <T> intindexOf(T[] list, T item)Searches specified array for specified item.static <T> T[]insert(T[] list, int index, T item)Equivalent toinsert(list, index, new T[] { item}).static <T> T[]insert(T[] list, int index, T[] items)Inserts specified items in specified array at specified index.static <T> T[]prepend(T[] list, T item)Inserts specified item in specified array at the beginning of specified array.static <T> T[]remove(T[] list, T item)Remove specified item from specified array.static <T> T[]removeAt(T[] list, int index)Equivalent toremove(list, index, 1).static <T> T[]removeAt(T[] list, int index, int count)Removes a range of items found at specified index from specified array.static <T> voidreverse(T[] list)Reverses the order of the items in the specified array.static <T> T[]subArray(T[] list, int start)Equivalent tosubArray(list, start, list.length).static <T> T[]subArray(T[] list, int start, int end)Returns a sub-array of specified array.static <T> T[]trimToSize(T[] list, int listSize)Returns specified array if its length is equal to specified size; otherwise returns a copy of specified array having specified size.
-
-
-
Method Detail
-
trimToSize
public static final <T> T[] trimToSize(T[] list, int listSize)Returns specified array if its length is equal to specified size; otherwise returns a copy of specified array having specified size.- Type Parameters:
T- the type of the components of the array- Parameters:
list- array whose size needs to be adjustedlistSize- the size of the returned array- Returns:
- an array having
listSizecomponents
-
subArray
public static final <T> T[] subArray(T[] list, int start)Equivalent tosubArray(list, start, list.length).
-
subArray
public static final <T> T[] subArray(T[] list, int start, int end)Returns a sub-array of specified array.- Type Parameters:
T- the type of the components of the array- Parameters:
list- the arraystart- the begin index, inclusiveend- the end index, exclusive- Returns:
- the sub-array
- Throws:
IndexOutOfBoundsException- ifstartorendare negative, ifendis greater thanlist.lengthor ifstartis greater thanend
-
insert
public static final <T> T[] insert(T[] list, int index, T item)Equivalent toinsert(list, index, new T[] { item}).
-
insert
public static final <T> T[] insert(T[] list, int index, T[] items)Inserts specified items in specified array at specified index.- Type Parameters:
T- the type of the components of the array- Parameters:
list- list to be modifiedindex- items are inserted at this position.Note that
indexmay be equal to the size of the array. This means: insert at end.items- items to be inserted- Returns:
- new array having original size+
items.lengthcomponents
-
prepend
public static final <T> T[] prepend(T[] list, T item)Inserts specified item in specified array at the beginning of specified array.- Type Parameters:
T- the type of the components of the array- Parameters:
list- list to be modifieditem- item to be inserted- Returns:
- new array having original size+1 components
-
append
public static final <T> T[] append(T[] list, T item)Inserts specified item in specified array at the end of specified array.- Type Parameters:
T- the type of the components of the array- Parameters:
list- list to be modifieditem- item to be inserted- Returns:
- new array having original size+1 components
-
remove
public static final <T> T[] remove(T[] list, T item)Remove specified item from specified array. Items are compared usingequals()and not==(usingindexOf(T[], T)).- Type Parameters:
T- the type of the components of the array- Parameters:
list- list to be modifieditem- item to be removed- Returns:
- new array having original size-1 components
-
indexOf
public static final <T> int indexOf(T[] list, T item)Searches specified array for specified item. Unlikefind(T[], T), items are compared usingequals()and not==.- Type Parameters:
T- the type of the components of the array- Parameters:
list- a list possibly containing itemitem- searched item- Returns:
- the index of searched item if found; -1 otherwise
-
find
public static final <T> int find(T[] list, T item)Searches specified array for specified item. UnlikeindexOf(T[], T), items are compared using==and notequals().- Type Parameters:
T- the type of the components of the array- Parameters:
list- a list possibly containing itemitem- searched item- Returns:
- the index of searched item if found; -1 otherwise
-
removeAt
public static final <T> T[] removeAt(T[] list, int index)Equivalent toremove(list, index, 1).
-
removeAt
public static final <T> T[] removeAt(T[] list, int index, int count)Removes a range of items found at specified index from specified array.- Type Parameters:
T- the type of the components of the array- Parameters:
list- list to be modifiedindex- items are removed at this positioncount- number of items to be removed- Returns:
- new array having original size-
count
-
reverse
public static final <T> void reverse(T[] list)
Reverses the order of the items in the specified array.
-
-