Class ArrayUtils
- java.lang.Object
-
- org.int4.dirk.org.apache.commons.lang3.ArrayUtils
-
public class ArrayUtils extends java.lang.ObjectOperations on arrays, primitive arrays (like
int[]) and primitive wrapper arrays (likeInteger[]).This class tries to handle
nullinput gracefully. An exception will not be thrown for anullarray input. However, an Object array that contains anullelement may throw an exception. Each method documents its behavior.#ThreadSafe#
- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description static boolean[]EMPTY_BOOLEAN_ARRAYAn empty immutablebooleanarray.static java.lang.Boolean[]EMPTY_BOOLEAN_OBJECT_ARRAYAn empty immutableBooleanarray.static char[]EMPTY_CHAR_ARRAYAn empty immutablechararray.static java.lang.Class<?>[]EMPTY_CLASS_ARRAYAn empty immutableClassarray.static int[]EMPTY_INT_ARRAYAn empty immutableintarray.static java.lang.String[]EMPTY_STRING_ARRAYAn empty immutableStringarray.
-
Constructor Summary
Constructors Constructor Description ArrayUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int[]add(int[] array, int element)Copies the given array and adds the given element at the end of the new array.static booleancontains(java.lang.Object[] array, java.lang.Object objectToFind)Checks if the object is in the given array.static booleanisSameLength(java.lang.Object array1, java.lang.Object array2)Checks whether two arrays are the same length, treatingnullarrays as length0.static <T> T[]remove(T[] array, int index)Removes the element at the specified position from the specified array.static <T> T[]removeAll(T[] array, int... indices)Removes the elements at the specified positions from the specified array.
-
-
-
Field Detail
-
EMPTY_BOOLEAN_ARRAY
public static final boolean[] EMPTY_BOOLEAN_ARRAY
An empty immutablebooleanarray.
-
EMPTY_BOOLEAN_OBJECT_ARRAY
public static final java.lang.Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY
An empty immutableBooleanarray.
-
EMPTY_CHAR_ARRAY
public static final char[] EMPTY_CHAR_ARRAY
An empty immutablechararray.
-
EMPTY_CLASS_ARRAY
public static final java.lang.Class<?>[] EMPTY_CLASS_ARRAY
An empty immutableClassarray.
-
EMPTY_INT_ARRAY
public static final int[] EMPTY_INT_ARRAY
An empty immutableintarray.
-
EMPTY_STRING_ARRAY
public static final java.lang.String[] EMPTY_STRING_ARRAY
An empty immutableStringarray.
-
-
Method Detail
-
add
public static int[] add(int[] array, int element)Copies the given array and adds the given element at the end of the new array.
The new array contains the same elements of the input array plus the given element in the last position. The component type of the new array is the same as that of the input array.
If the input array is
null, a new one element array is returned whose component type is the same as the element.ArrayUtils.add(null, 0) = [0] ArrayUtils.add([1], 0) = [1, 0] ArrayUtils.add([1, 0], 1) = [1, 0, 1]
- Parameters:
array- the array to copy and add the element to, may benullelement- the object to add at the last index of the new array- Returns:
- A new array containing the existing elements plus the new element
- Since:
- 2.1
-
contains
public static boolean contains(java.lang.Object[] array, java.lang.Object objectToFind)Checks if the object is in the given array.
The method returns
falseif anullarray is passed in.- Parameters:
array- the array to search throughobjectToFind- the object to find- Returns:
trueif the array contains the object
-
isSameLength
public static boolean isSameLength(java.lang.Object array1, java.lang.Object array2)Checks whether two arrays are the same length, treating
nullarrays as length0.Any multi-dimensional aspects of the arrays are ignored.
- Parameters:
array1- the first array, may benullarray2- the second array, may benull- Returns:
trueif length of arrays matches, treatingnullas an empty array- Since:
- 3.11
-
remove
public static <T> T[] remove(T[] array, int index)Removes the element at the specified position from the specified array. All subsequent elements are shifted to the left (subtracts 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(["a"], 0) = [] ArrayUtils.remove(["a", "b"], 0) = ["b"] ArrayUtils.remove(["a", "b"], 1) = ["a"] ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
- Type Parameters:
T- the component type of the array- Parameters:
array- the array to remove the element from, may not benullindex- the position of the element to be removed- Returns:
- A new array containing the existing elements except the element at the specified position.
- Throws:
java.lang.IndexOutOfBoundsException- if the index is out of range (index < 0 || index >= array.length), or if the array isnull.- Since:
- 2.1
-
removeAll
public static <T> T[] removeAll(T[] array, int... indices)Removes the elements at the specified positions from the specified array. All remaining elements are shifted to the left.
This method returns a new array with the same elements of the input array except those at the specified positions. 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.removeAll(["a", "b", "c"], 0, 2) = ["b"] ArrayUtils.removeAll(["a", "b", "c"], 1, 2) = ["a"]
- Type Parameters:
T- the component type of the array- Parameters:
array- the array to remove the element from, may not benullindices- the positions of the elements to be removed- Returns:
- A new array containing the existing elements except those at the specified positions.
- Throws:
java.lang.IndexOutOfBoundsException- if any index is out of range (index < 0 || index >= array.length), or if the array isnull.- Since:
- 3.0.1
-
-