public final class ArrayUtils extends Object
boolean[], byte[], Object[],
etc.). Most methods delegate to methods in Arrays with the appropriate signature. The methods in this class
are useful when dealing with arrays held as Object variable whose specific array type is unknown.
Methods throw a runtime exception if the given object is not an array. To check if an object is an array, use
object.getClass().isArray(). See individual methods for specific caveats.
| Modifier and Type | Method and Description |
|---|---|
static int |
binarySearch(Object array,
int fromIndex,
int toIndex,
Object key)
Delegates to the appropriate method for binary search and returns the result.
|
static int |
binarySearch(Object array,
Object key)
Delegates to the appropriate method for binary search and returns the result.
|
static Object |
copyOf(Object original,
int newLength)
Delegates to the appropriate method for creating a copy and returns the result.
|
static Object |
copyOfRange(Object original,
int from,
int to)
Delegates to the appropriate method for creating a copy of a range and returns the result.
|
static boolean |
equals(Object a,
Object a2)
Delegates to the appropriate method for checking whether two arrays have the same entries, and returns the
result.
|
static void |
fill(Object a,
int fromIndex,
int toIndex,
Object val)
Delegates to the appropriate method for filling an array with the given value.
|
static void |
fill(Object a,
Object val)
Delegates to the appropriate method for filling an array with the given value.
|
static int |
hashCode(Object a)
Delegates to the appropriate method for calculating the hash code, and returns the result.
|
static void |
parallelSort(Object a)
Delegates to the appropriate method to parallel-sort the given array.
|
static void |
parallelSort(Object a,
int fromIndex,
int toIndex)
Delegates to the appropriate method to parallel-sort the given array.
|
static int |
simpleBooleanArrayBinarySearch(boolean[] array,
int fromIndex,
int toIndex,
boolean value)
Fill-in implementation for a boolean array binary search: allows to search for a value in a boolean array that
has been sorted.
|
static void |
simpleBooleanArraySort(boolean[] array,
int fromIndex,
int toIndex)
Naive implementation of an array sort for boolean arrays.
|
static void |
sort(Object a)
Delegates to the appropriate method to sort the given array.
|
static void |
sort(Object a,
int fromIndex,
int toIndex)
Delegates to the appropriate method to sort the given array.
|
static Stream<Object> |
stream(Object array)
Returns a stream of the objects in the given array.
|
static String |
toString(Object a)
Delegates to the appropriate method to generate a String representation of the given array.
|
public static int binarySearch(Object array, Object key)
Arrays.binarySearch(byte[], int, int, byte),
Arrays.binarySearch(char[], int, int, char), etc. An exception is thrown if the key does not correspond
to the array type, or if it is null. All elements in Object[] arrays must implement Comparable
and be mutually comparable, or an exception will be thrown.
There is no binary search for boolean arrays in Arrays. This class provides a fill-in implementation.
array - the array object (e.g. String[] or double[])key - the key to search for (must match the array type and may not be null)Arrays.binarySearch(byte[], byte))public static int binarySearch(Object array, int fromIndex, int toIndex, Object key)
Arrays.binarySearch(byte[], int, int, byte),
Arrays.binarySearch(char[], int, int, char), etc. An exception is thrown if the key does not correspond
to the array type, or if it is null. All elements in Object[] arrays must implement Comparable,
and be mutually comparable, or an exception will be thrown.
There is no binary search for boolean arrays in Arrays. This class provides a fill-in implementation.
array - the array object (e.g. String[] or double[])fromIndex - start index of the range to search in (inclusive)toIndex - end index of the range to search in (exclusive)key - the key to search for (must match the array type and may not be null)Arrays.binarySearch(byte[], byte))public static Object copyOf(Object original, int newLength)
Arrays.copyOf(byte[], int), Arrays.copyOf(char[], int), etc.original - the array object to copy (e.g. String[] or double[])newLength - the length of the copypublic static Object copyOfRange(Object original, int from, int to)
Arrays.copyOfRange(byte[], int, int),
Arrays.copyOfRange(char[], int, int), etc.original - the array object to copy (e.g. String[] or double[])from - start index of the range to copy (inclusive)to - end index of the range to copy (exclusive)public static boolean equals(Object a, Object a2)
Arrays.equals(byte[], byte[]),
Arrays.equals(char[], char[]), etc.a - the array to check for equality (e.g. String[] or double[])a2 - the array to check the first array with (must have the same type)public static void fill(Object a, Object val)
Arrays.fill(byte[], int, int, byte), Arrays.fill(char[], int, int, char), etc.a - the array to fill (e.g. String[] or double[])val - the value to fill the array withpublic static void fill(Object a, int fromIndex, int toIndex, Object val)
Arrays.fill(byte[], int, int, byte), Arrays.fill(char[], int, int, char), etc.a - the array to fill (e.g. String[] or double[])fromIndex - start index of the range to fill with the value (inclusive)toIndex - end index of the range to fill with the value (exclusive)val - the value to fill the array withpublic static int hashCode(Object a)
Arrays.hashCode(byte[]), Arrays.hashCode(char[]), etc.a - the array to process (e.g. String[] or double[])public static void parallelSort(Object a)
Arrays.parallelSort(byte[], int, int), Arrays.parallelSort(char[], int, int), etc.
The sorting of Object[] arrays is delegated to Arrays.parallelSort(Comparable[], int, int),
which requires the array component to implement Comparable. All elements must be mutually comparable.
An exception is thrown otherwise.
There is no sorting method for boolean arrays in Arrays. This class provides a fill-in implementation,
which does not perform the search in parallel. Developers requiring more efficient sorting of boolean arrays
are suggested to use another method.
a - the array to sort (e.g. String[] or double[])public static void parallelSort(Object a, int fromIndex, int toIndex)
Arrays.parallelSort(byte[], int, int), Arrays.parallelSort(char[], int, int), etc.
The sorting of Object[] arrays is delegated to Arrays.parallelSort(Comparable[], int, int),
which requires the array component to implement Comparable. All elements must be mutually comparable.
An exception is thrown otherwise.
There is no sorting method for boolean arrays in Arrays. This class provides a fill-in implementation,
which does not perform the search in parallel. Developers requiring more efficient sorting of boolean arrays
are suggested to use another method.
a - the array to sort (e.g. String[] or double[])fromIndex - start index (inclusive) of the range that should be sortedtoIndex - end index (exclusive) of the range that should be sortedpublic static Stream<Object> stream(Object array)
Arrays, such as
Arrays.stream(int[]), return different types of streams, this method returns an object stream which is
constructed by getting the elements of the array by index.
It may be more efficient to handle array types individually with their respective stream types when performance is crucial.
array - the array to stream overpublic static void sort(Object a)
Arrays.sort(byte[], int, int), Arrays.sort(char[], int, int), etc.
The sorting of Object[] arrays is delegated to Arrays.sort(Object[], int, int),
which requires that all elements implement Comparable and that they be mutually comparable. An exception
is thrown otherwise.
There is no sorting method for boolean arrays in Arrays. This class provides a fill-in implementation.
Developers requiring more efficient sorting of boolean arrays are suggested to use another implementation.
a - the array to sort (e.g. String[] or double[])public static void sort(Object a, int fromIndex, int toIndex)
Arrays.sort(byte[], int, int), Arrays.sort(char[], int, int), etc.
The sorting of Object[] arrays is delegated to Arrays.sort(Object[], int, int),
which requires that all elements implement Comparable and that they be mutually comparable. An exception
is thrown otherwise.
There is no sorting method for boolean arrays in Arrays. This class provides a fill-in implementation.
Developers requiring more efficient sorting of boolean arrays are suggested to use another implementation.
a - the array to sort (e.g. String[] or double[])fromIndex - start index (inclusive) of the range that should be sortedtoIndex - end index (exclusive) of the range that should be sortedpublic static String toString(Object a)
Arrays.toString(byte[]), Arrays.toString(char[]), etc.a - the array to sort (e.g. String[] or double[])public static int simpleBooleanArrayBinarySearch(boolean[] array,
int fromIndex,
int toIndex,
boolean value)
Arrays (e.g. Arrays.binarySearch(char[], int, int, char)).array - the boolean array to search infromIndex - the start index (inclusive) of the range to search intoIndex - the end index (exclusive) of the range to search invalue - the value to search forArrays.binarySearch(char[], int, int, char))public static void simpleBooleanArraySort(boolean[] array,
int fromIndex,
int toIndex)
Background: Arrays has methods for sorting all array types (e.g. Arrays.sort(byte[], int, int))
except for boolean arrays.
array - the array to sortfromIndex - the start index (inclusive) of the range that should be sortedtoIndex - the end index (exclusive) of the range that should be sortedCopyright © 2023. All rights reserved.