public class ArrayUtils extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static int[] |
EMPTY_INT_ARRAY
An empty immutable
int array. |
static String[] |
EMPTY_STRING_ARRAY
An empty immutable
String array. |
static int |
INDEX_NOT_FOUND
The index value when an element is not found in a list or array:
-1. |
| 构造器和说明 |
|---|
ArrayUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static int[] |
add(int[] array,
int element)
Copies the given array and adds the given element at the end of the new array.
|
static <T> T[] |
add(T[] array,
T element)
Copies the given array and adds the given element at the end of the new array.
|
static boolean |
contains(Object[] array,
Object objectToFind)
Checks if the object is in the given array.
|
static int |
getLength(Object array)
Returns the length of the specified array.
|
static int |
indexOf(Object[] array,
Object objectToFind)
Finds the index of the given object in the array.
|
static int |
indexOf(Object[] array,
Object objectToFind,
int startIndex)
Finds the index of the given object in the array starting at the given index.
|
static <T> boolean |
isEmpty(T[] array)
Checks if an array of Objects is empty or
null. |
static <T> boolean |
isNotEmpty(T[] array)
Checks if an array of Objects is not empty or not
null. |
static boolean |
isSameLength(Object[] array1,
Object[] array2)
Checks whether two arrays are the same length, treating
null arrays as length 0. |
static int[] |
subarray(int[] array,
int startIndexInclusive,
int endIndexExclusive)
Produces a new
int array containing the elements
between the start and end indices. |
static <T> T[] |
subarray(T[] array,
int startIndexInclusive,
int endIndexExclusive)
Produces a new array containing the elements between
the start and end indices.
|
static String |
toString(Object array)
Outputs an array as a String, treating
null as an empty array. |
public static final String[] EMPTY_STRING_ARRAY
String array.public static final int[] EMPTY_INT_ARRAY
int array.public static <T> boolean isNotEmpty(T[] array)
Checks if an array of Objects is not empty or not null.
T - the component type of the arrayarray - the array to testtrue if the array is not empty or not nullpublic static <T> boolean isEmpty(T[] array)
Checks if an array of Objects is empty or null.
T - the component type of the arrayarray - the array to testtrue if the array is empty or nullpublic static <T> T[] subarray(T[] array,
int startIndexInclusive,
int endIndexExclusive)
Produces a new array containing the elements between the start and end indices.
The start index is inclusive, the end index exclusive. Null array input produces null output.
The component type of the subarray is always the same as
that of the input array. Thus, if the input is an array of type
Date, the following usage is envisaged:
Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
T - the component type of the arrayarray - the arraystartIndexInclusive - the starting index. Undervalue (<0)
is promoted to 0, overvalue (>array.length) results
in an empty array.endIndexExclusive - elements up to endIndex-1 are present in the
returned subarray. Undervalue (< startIndex) produces
empty array, overvalue (>array.length) is demoted to
array length.Arrays.copyOfRange(Object[], int, int)public static int[] subarray(int[] array,
int startIndexInclusive,
int endIndexExclusive)
Produces a new int array containing the elements
between the start and end indices.
The start index is inclusive, the end index exclusive. Null array input produces null output.
array - the arraystartIndexInclusive - the starting index. Undervalue (<0)
is promoted to 0, overvalue (>array.length) results
in an empty array.endIndexExclusive - elements up to endIndex-1 are present in the
returned subarray. Undervalue (< startIndex) produces
empty array, overvalue (>array.length) is demoted to
array length.Arrays.copyOfRange(int[], int, int)public static int getLength(Object array)
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
array - the array to retrieve the length from, may be null0 if the array is nullIllegalArgumentException - if the object argument is not an array.public static <T> T[] add(T[] array,
T 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, unless the element itself is null,
in which case the return type is Object[]
ArrayUtils.add(null, null) = [null] ArrayUtils.add(null, "a") = ["a"] ArrayUtils.add(["a"], null) = ["a", null] ArrayUtils.add(["a"], "b") = ["a", "b"] ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
T - the component type of the arrayarray - the array to "add" the element to, may be nullelement - the object to add, may be nullIllegalArgumentException - if both arguments are nullpublic 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]
array - the array to copy and add the element to, may be nullelement - the object to add at the last index of the new arraypublic static boolean isSameLength(Object[] array1, Object[] array2)
Checks whether two arrays are the same length, treating
null arrays as length 0.
Any multi-dimensional aspects of the arrays are ignored.
array1 - the first array, may be nullarray2 - the second array, may be nulltrue if length of arrays matches, treating
null as an empty arraypublic static String toString(Object array)
Outputs an array as a String, treating null as an empty array.
Multi-dimensional arrays are handled correctly, including multi-dimensional primitive arrays.
The format is that of Java source code, for example {a,b}.
array - the array to get a toString for, may be nullpublic static boolean contains(Object[] array, Object objectToFind)
Checks if the object is in the given array.
The method returns false if a null array is passed in.
array - the array to search throughobjectToFind - the object to findtrue if the array contains the objectpublic static int indexOf(Object[] array, Object objectToFind)
Finds the index of the given object in the array.
This method returns INDEX_NOT_FOUND (-1) for a null input array.
array - the array to search through for the object, may be nullobjectToFind - the object to find, may be nullINDEX_NOT_FOUND (-1) if not found or null array inputpublic static int indexOf(Object[] array, Object objectToFind, int startIndex)
Finds the index of the given object in the array starting at the given index.
This method returns 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 INDEX_NOT_FOUND (-1).
array - the array to search through for the object, may be nullobjectToFind - the object to find, may be nullstartIndex - the index to start searching atINDEX_NOT_FOUND (-1) if not found or null array inputCopyright © 2015. All rights reserved.