Operations on arrays, primitive arrays (like int[]
) and
* primitive wrapper arrays (like Integer[]
).
This class tries to handle null
input gracefully.
* An exception will not be thrown for a null
* array input. However, an Object array that contains a null
* element may throw an exception. Each method documents its behaviour.
Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).
* *This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.
* *If the input array is null
, an IndexOutOfBoundsException
* will be thrown, because in that case no valid index can be specified.
* ArrayUtils.remove([1], 0) = [] * ArrayUtils.remove([2, 6], 0) = [6] * ArrayUtils.remove([2, 6], 1) = [2] * ArrayUtils.remove([2, 6, 3], 1) = [2, 3] ** * @param array the array to remove the element from, may not be
null
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index >= array.length), or if the array is null
.
* @since 2.1
*/
public static short[] remove(short[] array, int index) {
return (short[]) remove((Object) array, index);
}
/**
* Removes the element at the specified position from the specified array. * All subsequent elements are shifted to the left (substracts one from * their indices).
* *This method returns a new array with the same elements of the input * array except the element on the specified position. The component * type of the returned array is always the same as that of the input * array.
* *If the input array is null
, an IndexOutOfBoundsException
* will be thrown, because in that case no valid index can be specified.
null
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index >= array.length), or if the array is null
.
* @since 2.1
*/
private static Object remove(Object array, int index) {
int length = getLength(array);
if (index < 0 || index >= length) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
}
Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
System.arraycopy(array, 0, result, 0, index);
if (index < length - 1) {
System.arraycopy(array, index + 1, result, index, length - index - 1);
}
return result;
}
/**
* Finds the index of the given value in the array starting at the given index.
* *This method returns {@link #INDEX_NOT_FOUND} (-1
) for a null
input array.
A negative startIndex is treated as zero. A startIndex larger than the array
* length will return {@link #INDEX_NOT_FOUND} (-1
).
null
* @param valueToFind the value to find
* @param startIndex the index to start searching at
* @return the index of the value within the array,
* {@link #INDEX_NOT_FOUND} (-1
) if not found or null
array input
*/
public static int indexOf(short[] array, short valueToFind) {
if (array == null) {
return -1;
}
for (int i = 0; i < array.length; i++) {
if (valueToFind == array[i]) {
return i;
}
}
return -1;
}
/**
* Returns the length of the specified array.
* This method can deal with Object
arrays and with primitive arrays.
If the input array is null
, 0
is returned.
* ArrayUtils.getLength(null) = 0 * ArrayUtils.getLength([]) = 0 * ArrayUtils.getLength([null]) = 1 * ArrayUtils.getLength([true, false]) = 2 * ArrayUtils.getLength([1, 2, 3]) = 3 * ArrayUtils.getLength(["a", "b", "c"]) = 3 ** * @param array the array to retrieve the length from, may be null * @return The length of the array, or
0
if the array is null
* @throws IllegalArgumentException if the object arguement is not an array.
* @since 2.1
*/
public static int getLength(Object array) {
if (array == null) {
return 0;
}
return Array.getLength(array);
}
/**
* Shallow clones an array returning a typecast result and handling
* null
.
The objects in the array are not cloned, thus there is no special * handling for multi-dimensional arrays.
* *This method returns null
for a null
input array.
null
* @return the cloned array, null
if null
input
*/
public static short[] clone(short[] array) {
if (array == null) {
return null;
}
return (short[]) array.clone();
}
}