public final class ArrayUtils extends Object
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.
This class's source was modified from the Apache commons-lang library: https://github.com/apache/commons-lang/
#ThreadSafe#
Modifier and Type | Field and Description |
---|---|
static int |
INDEX_NOT_FOUND |
Modifier and Type | Method and Description |
---|---|
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 boolean |
isEmpty(Object[] 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 and not
null . |
static int |
lastIndexOf(Object[] array,
Object objectToFind)
Finds the last index of the given object within the array.
|
public static final int INDEX_NOT_FOUND
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 null
IllegalArgumentException
- if the object argument is not an array.public 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 null
objectToFind
- the object to find, may be null
INDEX_NOT_FOUND
(-1
) if not found or null
array inputpublic static int lastIndexOf(Object[] array, Object objectToFind)
Finds the last index of the given object within the array.
This method returns INDEX_NOT_FOUND
(-1
) for a null
input array.
array
- the array to traverse backwards looking for the object, may be null
objectToFind
- the object to find, may be null
INDEX_NOT_FOUND
(-1
) if not found or null
array inputpublic 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 boolean isEmpty(Object[] array)
Checks if an array of Objects is empty or null
.
array
- the array to testtrue
if the array is empty or null
public static <T> boolean isNotEmpty(T[] array)
Checks if an array of Objects is not empty and not null
.
T
- the component type of the arrayarray
- the array to testtrue
if the array is not empty and not null
Copyright © 2017 Amazon Web Services, Inc. All Rights Reserved.