public abstract class ArrayUtils extends Object
ArrayUtils
is an abstract utility class used to work with Object
arrays.Modifier and Type | Class and Description |
---|---|
protected static class |
ArrayUtils.IterableArray<T> |
Constructor and Description |
---|
ArrayUtils() |
Modifier and Type | Method and Description |
---|---|
static <T> T[] |
asArray(T... elements)
Returns the given varargs
element as an array. |
static <T> T[] |
defaultIfEmpty(T[] array,
T[] defaultArray)
Returns the given
array if not null or empty, otherwise returns the defaultArray . |
static <T> T |
getFirst(T[] array)
Null-safe method to return the first element in the array or null
if the array is null or empty.
|
static <T> T |
getFirst(T[] array,
T defaultValue)
Null-safe method to return the first element in the array or the
defaultValue
if the array is null or empty. |
static Object[] |
insert(Object[] originalArray,
int position,
Object element)
Insert an element into the given array at position (index).
|
static boolean |
isEmpty(Object[] array)
Determines whether the given array is empty or not.
|
static boolean |
isNotEmpty(Object[] array)
Determines whether the given array is empty or not.
|
static int |
length(Object[] array)
Null-safe operation to determine an array's length.
|
static <T> T[] |
nullSafeArray(T[] array,
Class<T> componentType)
Null-safe, empty array operation returning the given object array if not null or an empty object array
if the array argument is null.
|
static Object[] |
remove(Object[] originalArray,
int position)
Remove an element from the given array at position (index).
|
static <T extends Comparable<T>> |
sort(T[] array)
Sort the array of elements according to the elements natural ordering.
|
static <T> Iterable<T> |
toIterable(T... array)
Converts the given array into an
Iterable object. |
@SafeVarargs public static <T> T[] asArray(T... elements)
element
as an array.T
- Class type of the elements.elements
- variable list of arguments to return as an array.elements
.@Nullable public static <T> T[] defaultIfEmpty(@Nullable T[] array, @Nullable T[] defaultArray)
array
if not null or empty, otherwise returns the defaultArray
.T
- Class
type of the array elements.array
- array to evaluate.defaultArray
- array to return if the given array
is null or empty.array
if not null or empty otherwise return the defaultArray
.@Nullable public static <T> T getFirst(@Nullable T[] array)
T
- Class type of the array elements.array
- the array from which to extract the first element.getFirst(Object[], Object)
@Nullable public static <T> T getFirst(@Nullable T[] array, @Nullable T defaultValue)
defaultValue
if the array is null or empty.T
- Class type of the array elements.array
- the array from which to extract the first element.defaultValue
- value to return if the array is null or empty.defaultValue
if the array is null or empty.getFirst(Object[])
@NonNull public static Object[] insert(@NonNull Object[] originalArray, int position, Object element)
originalArray
- the array in which to insert the element.position
- an integer index (position) at which to insert the element in the array.element
- the element to insert into the array.System.arraycopy(Object, int, Object, int, int)
,
Array.newInstance(Class, int)
public static boolean isEmpty(@Nullable Object[] array)
array
- the array to evaluate for emptiness.length(Object...)
public static boolean isNotEmpty(@Nullable Object[] array)
array
- the array to evaluate for emptiness.isEmpty(Object[])
public static int length(@Nullable Object[] array)
array
- the array to determine it's length.public static <T> T[] nullSafeArray(@Nullable T[] array, @NonNull Class<T> componentType)
T
- Class type of the array elements.array
- array of objects on which a null check is performed.componentType
- Class type of the array elements.Array.newInstance(Class, int)
public static Object[] remove(@NonNull Object[] originalArray, int position)
originalArray
- the array from which to remove the element.position
- the integer index (position) indicating the element to remove from the array.System.arraycopy(Object, int, Object, int, int)
,
Array.newInstance(Class, int)
@NonNull public static <T extends Comparable<T>> T[] sort(@NonNull T[] array)
T
- Comparable
class type of the array elements.array
- array of elements to sort.Arrays.sort(Object[])
@NonNull public static <T> Iterable<T> toIterable(@NonNull T... array)
Iterable
object.T
- type
of the array elements; mut not be null.array
- array to convert to an Iterable
.Iterable
object from the given array.IllegalArgumentException
- if the array is null.Iterable
Copyright © 2011–2023 Pivotal Software, Inc.. All rights reserved.