public abstract class ArrayUtil extends Object
Constructor and Description |
---|
ArrayUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> T[] |
addAll(Class<T> c,
T[] dest,
Object[] source)
Takes contents of the two arrays up to the first
null element and concatenates
them. |
static <T> void |
addAll(Collection<? super T> collection,
T... elements)
Adds all elements of an array to a collection.
|
static <T> T[] |
addAll(T[] dest,
Object[] source)
Takes contents of the two arrays up to the first
null element and concatenates them. |
static Object[] |
append(Class<?> c,
Object[] array,
int currentLength,
Object obj)
Deprecated.
Use
appendAt(Class, Object[], int, Object) instead. |
static <T> T[] |
append(Class<T> c,
T[] array,
T obj)
Assumes that array contains
null s at the end, only. |
static <T> T[] |
append(T[] array,
T obj)
Assumes that array contains
null s at the end, only. |
static <T> T[] |
appendAt(Class<T> c,
T[] array,
int currentLength,
T obj)
Assumes that array contains
null s at the end, only. |
static <T> T[] |
appendAt(T[] array,
int currentLength,
T obj)
Assumes that array contains
null s at the end, only. |
static void |
compact(Object[] array)
Moves all
null elements to the end of the array. |
static <T> boolean |
contains(T[] array,
T obj)
Returns whether the specified array contains the specified object.
|
static <T> boolean |
containsEqual(T[] array,
T obj)
Assumes that array contains
null s at the end, only. |
static <S,T> T[] |
convert(Class<T> target,
S[] source)
Stores the specified array contents in a new array of specified runtime type.
|
static <T> T[] |
filter(T[] array,
IUnaryPredicate<T> predicate)
Filter the elements of an array down to just the ones
that match the given predicate.
|
static <T> int |
indexOf(T[] array,
T obj)
Returns the index into the specified array of the specified object, or -1 if the array does
not contain the object, or if the array is
null . |
static <T> int |
indexOfEqual(T[] array,
T obj)
Assumes that array contains
null s at the end, only. |
static <T> T[] |
prepend(Class<T> c,
T[] array,
T obj)
Inserts the
obj at the beginning of the array, shifting the whole thing one index
Assumes that array contains null s at the end, only. |
static <T> T[] |
prepend(T[] array,
T obj)
Inserts the
obj at the beginning of the array, shifting the whole thing one index
Assumes that array contains null s at the end, only. |
static <T> void |
remove(T[] array,
T element)
Removes first occurrence of element in array and moves objects behind up front.
|
static <T> T[] |
removeDuplicates(T[] array)
Returns a new array that contains elements of the given array except duplicates and
null s. |
static <T> T[] |
removeFirst(T[] array)
Returns a new array that contains all of the elements of the given array except
the first one.
|
static <T> T[] |
removeNulls(Class<T> c,
T[] array)
Removes all of the
null s from the array and returns a new array that contains all
of the non-null elements. |
static <T> T[] |
removeNulls(T[] array)
Removes all of the
null s from the array and returns a new array that contains all
of the non-null elements. |
static Object[] |
removeNullsAfter(Class<?> c,
Object[] array,
int index)
Deprecated.
Use
trim(Object[], int) or trimAt(Class, Object[], int) instead |
static void |
reverse(Object[] array)
Reverses order of elements in an array.
|
static void |
reverse(Object[] array,
int fromIndex,
int toIndex)
Reverses order of elements in a subsection of an array.
|
static int[] |
setInt(int[] array,
int idx,
int val) |
static <T> T[] |
trim(Class<T> c,
T[] array) |
static <T> T[] |
trim(Class<T> c,
T[] array,
boolean forceNew)
Trims the given array and returns a new array with no
null entries. |
static <T> T[] |
trim(T[] array)
Trims the given array and returns a new array with no
null entries. |
static <T> T[] |
trim(T[] array,
boolean forceNew)
Trims the given array and returns a new array with no
null entries. |
static <T> T[] |
trim(T[] array,
int newLength)
Trims the given array and returns a new array with no
null entries. |
static <T> T[] |
trimAt(Class<T> c,
T[] array,
int index)
To improve performance, this method should be used instead of
removeNulls(Class, Object[]) when all of the non-null elements in
the array are grouped together at the beginning of the array and all of the s are at
the end of the array. |
public static <T> T[] append(Class<T> c, T[] array, T obj)
null
s at the end, only.
Appends element after the last non-null
element.
If the array is null
or not large enough, a larger one is allocated, using
the given class object.public static <T> T[] append(T[] array, T obj)
null
s at the end, only.
Appends element after the last non-null
element.
If the array is not large enough, a larger one is allocated.
Null array
is supported for backward compatibility only and only when T is
Object.@Deprecated public static Object[] append(Class<?> c, Object[] array, int currentLength, Object obj)
appendAt(Class, Object[], int, Object)
instead.public static <T> T[] appendAt(Class<T> c, T[] array, int currentLength, T obj)
null
s at the end, only.
Appends object using the current length of the array.public static <T> T[] appendAt(T[] array, int currentLength, T obj)
null
s at the end, only.
Appends object using the current length of the array.array
- The array to append to. Not null
currentLength
- The number of non-null
elements in the arrayobj
- The object to append. Not null
public static <T> T[] trim(Class<T> c, T[] array, boolean forceNew)
null
entries.
Assumes that null
s can be found at the end, only.
if array
is null
, a new array of length 0 is returned
if forceNew
is true
, a new array will always be created.
if forceNew
is false
, a new array will only be created if the original array
contained null
entries.c
- the type of the new arrayarray
- the array to be trimmedforceNew
- public static <T> T[] trim(Class<T> c, T[] array)
public static <T> T[] trim(T[] array, boolean forceNew)
null
entries.
Assumes that null
s can be found at the end, only.
if forceNew
is true
, a new array will always be created.
if forceNew
is false
, a new array will only be created if the original array
contained null
entries.array
- the array to be trimmedforceNew
- public static <T> T[] trim(T[] array)
null
entries.
Assumes that null
s can be found at the end, only.array
- the array to be trimmedpublic static <T> T[] trim(T[] array, int newLength)
null
entries.
Assumes that null
s can be found at the end, only.
Similar to trimAt(Class, Object[], int)
, but uses the new length instead of index.array
- the array to be trimmednewLength
- the new length of the array, has to be less or equal than
the current length.public static <T> T[] addAll(Class<T> c, T[] dest, Object[] source)
null
element and concatenates
them.c
- The type of the element of the returned array if there was not enough free space
in the destination array.dest
- The destination array. The elements of the source array are added to this array
if there is enough free space in it. May be null
.source
- The source array. May not be null
.public static <T> T[] addAll(T[] dest, Object[] source)
null
element and concatenates them.dest
- The destination array. The elements of the source array are added to this array
if there is enough free space in it. May be null
.source
- The source array. May not be null
.@SafeVarargs public static <T> void addAll(Collection<? super T> collection, T... elements)
ArrayList
this method is
slightly more efficient than java.util.Collections#addAll(Collection, T...)
.public static <T> boolean contains(T[] array, T obj)
array
- the array to searchobj
- the object to search fortrue
if the specified array contains the specified object, or
the specified array is null
public static <T> int indexOf(T[] array, T obj)
null
. Comparison is by object identity.array
- the array to searchobj
- the object to search fornull
public static <T> boolean containsEqual(T[] array, T obj)
null
s at the end, only.
Returns whether the specified array contains the specified object. Comparison is by
object identity.array
- the array to searchobj
- the object to search fornull
public static <T> int indexOfEqual(T[] array, T obj)
null
s at the end, only.
Returns the index into the specified array of the specified object, or -1 if the array does
not contain the object, or if the array is null
. Comparison is by equals().array
- the array to searchobj
- the object to search fornull
public static void compact(Object[] array)
null
elements to the end of the array. The order of non-null
elements is preserved.public static <T> T[] removeNulls(Class<T> c, T[] array)
null
s from the array and returns a new array that contains all
of the non-null
elements.
If there are no null
s in the original array then the original array is returned.
Note that this method should only be used when the placement of null
s within
the array is unknown (due to performance efficiency).public static <T> T[] removeNulls(T[] array)
null
s from the array and returns a new array that contains all
of the non-null
elements.
If there are no null
s in the original array then the original array is returned.
Note that this method should only be used when the placement of null
s within
the array is unknown (due to performance efficiency).
@Deprecated public static Object[] removeNullsAfter(Class<?> c, Object[] array, int index)
trim(Object[], int)
or trimAt(Class, Object[], int)
insteadpublic static <T> T[] trimAt(Class<T> c, T[] array, int index)
removeNulls(Class, Object[])
when all of the non-null
elements in
the array are grouped together at the beginning of the array and all of the s are at
the end of the array. The position of the last non-null
element in the array must
also be known.
If you don't intend to pass null
array, consider using trim(Object[], int)
instead.
public static <T> T[] prepend(Class<T> c, T[] array, T obj)
obj
at the beginning of the array, shifting the whole thing one index
Assumes that array contains null
s at the end, only.public static <T> T[] prepend(T[] array, T obj)
obj
at the beginning of the array, shifting the whole thing one index
Assumes that array contains null
s at the end, only.
array must not be null
.public static <T> void remove(T[] array, T element)
public static <S,T> T[] convert(Class<T> target, S[] source)
target
- the runtime type of the new arraysource
- the source arraynull
if source is null
.public static void reverse(Object[] array)
array
- the arraypublic static void reverse(Object[] array, int fromIndex, int toIndex)
array
- the arrayfromIndex
- the index of the first affected element (inclusive)toIndex
- the index of the last affected element (exclusive)public static <T> T[] removeFirst(T[] array)
NullPointerException
- if array
is null
IllegalArgumentException
- if array
is emptypublic static <T> T[] removeDuplicates(T[] array)
null
s. Duplicates are determined by Object.equals(Object)
method.NullPointerException
- if array
is null
public static int[] setInt(int[] array, int idx, int val)
public static <T> T[] filter(T[] array, IUnaryPredicate<T> predicate)
Copyright (c) IBM Corp. and others 2004, 2020. All Rights Reserved.