public final class ArraysExt extends Static
Arrays
utility class.
Some worthy methods are:
resize
methods, which are very similar to the
Arrays.copyOf(…)
methods except that they accept
null
arrays and do not copy anything if the given array already has the
requested length.insert
and remove
methods for adding and removing elements in the middle of an array.isSorted
methods for verifying
if an array is sorted, strictly or not.All methods in this class are tolerant to null elements in arrays. Null and NaN elements are ignored.
Arrays
Defined in the sis-utility module
Modifier and Type | Field and Description |
---|---|
static boolean[] |
EMPTY_BOOLEAN
An empty array of
boolean primitive type. |
static byte[] |
EMPTY_BYTE
An empty array of
byte primitive type. |
static char[] |
EMPTY_CHAR
An empty array of
char primitive type. |
static double[] |
EMPTY_DOUBLE
An empty array of
double primitive type. |
static float[] |
EMPTY_FLOAT
An empty array of
float primitive type. |
static int[] |
EMPTY_INT
An empty array of
int primitive type. |
static long[] |
EMPTY_LONG
An empty array of
long primitive type. |
static short[] |
EMPTY_SHORT
An empty array of
short primitive type. |
Modifier and Type | Method and Description |
---|---|
static boolean |
allEquals(double[] array,
double value)
Returns
true if all values in the specified array are equal to the specified
value, which may be Double.NaN . |
static boolean |
allEquals(float[] array,
float value)
Returns
true if all values in the specified array are equal to the specified
value, which may be Float.NaN . |
static <T> T[] |
append(T[] array,
T element)
Returns a copy of the given array with a single element appended at the end.
|
static <T> T[] |
concatenate(T[]... arrays)
Returns the concatenation of all given arrays.
|
static boolean |
contains(Object[] array,
Object value)
Returns
true if the specified array contains the specified value. |
static boolean |
containsIdentity(Object[] array,
Object value)
Returns
true if the specified array contains the specified reference. |
static boolean |
containsIgnoreCase(String[] array,
String value)
Returns
true if the specified array contains the specified value, ignoring case. |
static float[] |
copyAsFloats(double[] data)
Returns a copy of the given array where each value has been casted to the
float type. |
static int[] |
copyAsInts(double[] data)
Returns a copy of the given array where each value has been
rounded to the
int type. |
static boolean |
hasNaN(double[] array)
Returns
true if the specified array contains at least one
NaN value. |
static boolean |
hasNaN(float[] array)
Returns
true if the specified array contains at least one
NaN value. |
static boolean[] |
insert(boolean[] src,
int srcOff,
boolean[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static boolean[] |
insert(boolean[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static byte[] |
insert(byte[] src,
int srcOff,
byte[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static byte[] |
insert(byte[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static char[] |
insert(char[] src,
int srcOff,
char[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static char[] |
insert(char[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static double[] |
insert(double[] src,
int srcOff,
double[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static double[] |
insert(double[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static <E> E[] |
insert(E[] src,
int srcOff,
E[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static <E> E[] |
insert(E[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static float[] |
insert(float[] src,
int srcOff,
float[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static float[] |
insert(float[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static int[] |
insert(int[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static int[] |
insert(int[] src,
int srcOff,
int[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static long[] |
insert(long[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static long[] |
insert(long[] src,
int srcOff,
long[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static short[] |
insert(short[] array,
int first,
int length)
Returns an array containing the same elements than the given array, with additional
"spaces" in the given range.
|
static short[] |
insert(short[] src,
int srcOff,
short[] dst,
int dstOff,
int length)
Returns an array containing the same elements than the given array, with the content
of an other array inserted at the given index.
|
static boolean |
intersects(Object[] array1,
Object[] array2)
Returns
true if at least one element in the first array is equals to an element in the second array. |
static boolean |
isSorted(byte[] array,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static boolean |
isSorted(char[] array,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static boolean |
isSorted(double[] array,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static <E extends Comparable<? super E>> |
isSorted(E[] array,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static <E> boolean |
isSorted(E[] array,
Comparator<? super E> comparator,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static boolean |
isSorted(float[] array,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static boolean |
isSorted(int[] array,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static boolean |
isSorted(long[] array,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static boolean |
isSorted(short[] array,
boolean strict)
Returns
true if all elements in the specified array are in increasing order. |
static boolean[] |
remove(boolean[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static byte[] |
remove(byte[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static char[] |
remove(char[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static double[] |
remove(double[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static <E> E[] |
remove(E[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static float[] |
remove(float[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static int[] |
remove(int[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static long[] |
remove(long[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static short[] |
remove(short[] array,
int first,
int length)
Returns an array containing the same elements than the given array except for
the given range.
|
static int |
removeDuplicated(Object[] array)
Removes the duplicated elements in the given array.
|
static boolean[] |
resize(boolean[] array,
int length)
Returns an array containing the same elements as the given
array but
specified length , truncating or padding with false if necessary. |
static byte[] |
resize(byte[] array,
int length)
Returns an array containing the same elements as the given
array but
specified length , truncating or padding with zeros if necessary. |
static char[] |
resize(char[] array,
int length)
Returns an array containing the same elements as the given
array but
specified length , truncating or padding with zeros if necessary. |
static double[] |
resize(double[] array,
int length)
Returns an array containing the same elements as the given
array but
specified length , truncating or padding with zeros if necessary. |
static <E> E[] |
resize(E[] array,
int length)
Returns an array containing the same elements as the given
array but with the
specified length , truncating or padding with null if necessary. |
static float[] |
resize(float[] array,
int length)
Returns an array containing the same elements as the given
array but
specified length , truncating or padding with zeros if necessary. |
static int[] |
resize(int[] array,
int length)
Returns an array containing the same elements as the given
array but
specified length , truncating or padding with zeros if necessary. |
static long[] |
resize(long[] array,
int length)
Returns an array containing the same elements as the given
array but
specified length , truncating or padding with zeros if necessary. |
static short[] |
resize(short[] array,
int length)
Returns an array containing the same elements as the given
array but
specified length , truncating or padding with zeros if necessary. |
static void |
reverse(int[] values)
Reverses the order of elements in the given array.
|
static void |
reverse(Object[] entries)
Reverses the order of elements in the given array.
|
static int[] |
unionOfSorted(int[] array1,
int[] array2)
Returns the union of two sorted arrays.
|
public static final double[] EMPTY_DOUBLE
double
primitive type.
Such arrays are immutable and can be safely shared.public static final float[] EMPTY_FLOAT
float
primitive type.
Such arrays are immutable and can be safely shared.public static final long[] EMPTY_LONG
long
primitive type.
Such arrays are immutable and can be safely shared.public static final int[] EMPTY_INT
int
primitive type.
Such arrays are immutable and can be safely shared.public static final short[] EMPTY_SHORT
short
primitive type.
Such arrays are immutable and can be safely shared.public static final byte[] EMPTY_BYTE
byte
primitive type.
Such arrays are immutable and can be safely shared.public static final char[] EMPTY_CHAR
char
primitive type.
Such arrays are immutable and can be safely shared.public static final boolean[] EMPTY_BOOLEAN
boolean
primitive type.
Such arrays are immutable and can be safely shared.public static <E> E[] resize(E[] array, int length) throws NegativeArraySizeException
array
but with the
specified length
, truncating or padding with null
if necessary.
length
is longer than the length of the given array
,
then the returned array will contain all the elements of array
at index
i < array.length
. Elements at index
i >= array.length
are initialized to null
.length
is shorter than the length of the given array
,
then the returned array will contain only the elements of array
at index
i < length
. Remaining elements are not copied.length
is equal to the length of the given array
,
then array
is returned unchanged. No copy is performed.
This behavior is different than the Arrays.copyOf(Object[], int)
one.null
, then this method unconditionally returns
null
no matter the value of the length
argument.E
- The array elements.array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(Object[], int)
public static double[] resize(double[] array, int length) throws NegativeArraySizeException
array
but
specified length
, truncating or padding with zeros if necessary.
This method returns null
if and only if the given array is null
,
in which case the value of the length
argument is ignored.
array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(double[], int)
public static float[] resize(float[] array, int length) throws NegativeArraySizeException
array
but
specified length
, truncating or padding with zeros if necessary.
This method returns null
if and only if the given array is null
,
in which case the value of the length
argument is ignored.
array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(float[], int)
public static long[] resize(long[] array, int length) throws NegativeArraySizeException
array
but
specified length
, truncating or padding with zeros if necessary.
This method returns null
if and only if the given array is null
,
in which case the value of the length
argument is ignored.
array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(long[], int)
public static int[] resize(int[] array, int length) throws NegativeArraySizeException
array
but
specified length
, truncating or padding with zeros if necessary.
This method returns null
if and only if the given array is null
,
in which case the value of the length
argument is ignored.
array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(int[], int)
public static short[] resize(short[] array, int length) throws NegativeArraySizeException
array
but
specified length
, truncating or padding with zeros if necessary.
This method returns null
if and only if the given array is null
,
in which case the value of the length
argument is ignored.
array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(short[], int)
public static byte[] resize(byte[] array, int length) throws NegativeArraySizeException
array
but
specified length
, truncating or padding with zeros if necessary.
This method returns null
if and only if the given array is null
,
in which case the value of the length
argument is ignored.
array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(byte[], int)
public static char[] resize(char[] array, int length) throws NegativeArraySizeException
array
but
specified length
, truncating or padding with zeros if necessary.
This method returns null
if and only if the given array is null
,
in which case the value of the length
argument is ignored.
array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(char[], int)
public static boolean[] resize(boolean[] array, int length) throws NegativeArraySizeException
array
but
specified length
, truncating or padding with false
if necessary.
This method returns null
if and only if the given array is null
,
in which case the value of the length
argument is ignored.
array
- Array to resize, or null
.length
- Length of the desired array.array
if the given
array is null
or already have the requested length.NegativeArraySizeException
- If length
is negative.Arrays.copyOf(boolean[], int)
public static <E> E[] remove(E[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns
the array
reference unchanged (except if empty). Otherwise this method
creates a new array. In every cases, the given array is never modified.
E
- The type of array elements.array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(Object[], int, int)
public static double[] remove(double[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged, except for empty arrays. Otherwise
this method creates a new array. In every cases, the given array is never modified.
array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(double[], int, int)
public static float[] remove(float[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged, except for empty arrays. Otherwise
this method creates a new array. In every cases, the given array is never modified.
array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(float[], int, int)
public static long[] remove(long[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged, except for empty arrays. Otherwise
this method creates a new array. In every cases, the given array is never modified.
array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(long[], int, int)
public static int[] remove(int[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged, except for empty arrays. Otherwise
this method creates a new array. In every cases, the given array is never modified.
array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(int[], int, int)
public static short[] remove(short[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged, except for empty arrays. Otherwise
this method creates a new array. In every cases, the given array is never modified.
array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(short[], int, int)
public static byte[] remove(byte[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged, except for empty arrays. Otherwise
this method creates a new array. In every cases, the given array is never modified.
array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(byte[], int, int)
public static char[] remove(char[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged, except for empty arrays. Otherwise
this method creates a new array. In every cases, the given array is never modified.
array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(char[], int, int)
public static boolean[] remove(boolean[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged, except for empty arrays. Otherwise
this method creates a new array. In every cases, the given array is never modified.
array
- Array from which to remove elements. Can be null
only if length
is 0.first
- Index of the first element to remove from the given array
.length
- Number of elements to remove.array
except for the
removed elements, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(boolean[], int, int)
public static <E> E[] insert(E[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
null
elements.
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
E
- The type of array elements.array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(Object[], int, Object[], int, int)
,
remove(Object[], int, int)
public static double[] insert(double[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(double[], int, double[], int, int)
,
remove(double[], int, int)
public static float[] insert(float[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(float[], int, float[], int, int)
,
remove(float[], int, int)
public static long[] insert(long[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(long[], int, long[], int, int)
,
remove(long[], int, int)
public static int[] insert(int[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(int[], int, int[], int, int)
,
remove(int[], int, int)
public static short[] insert(short[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(short[], int, short[], int, int)
,
remove(short[], int, int)
public static byte[] insert(byte[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(byte[], int, byte[], int, int)
,
remove(byte[], int, int)
public static char[] insert(char[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(char[], int, char[], int, int)
,
remove(char[], int, int)
public static boolean[] insert(boolean[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
false
.
If the length
argument is 0, then this method returns the array
reference unchanged. Otherwise this method creates a new array. In every cases,
the given array is never modified.
array
- Array in which to insert spaces. Can be null
only if length
is 0.first
- Index where the first space should be inserted. All array
elements
having an index equal to or higher than index
will be moved forward.length
- Number of spaces to insert.array
elements with the additional space
inserted, or array
(which may be null) if length
is 0.NullArgumentException
- If array
is null and length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If first
or first+length
is out of array bounds.insert(boolean[], int, boolean[], int, int)
,
remove(boolean[], int, int)
public static <E> E[] insert(E[] src, int srcOff, E[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
E
- The type of array elements.src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static double[] insert(double[] src, int srcOff, double[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static float[] insert(float[] src, int srcOff, float[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static long[] insert(long[] src, int srcOff, long[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static int[] insert(int[] src, int srcOff, int[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static short[] insert(short[] src, int srcOff, short[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static byte[] insert(byte[] src, int srcOff, byte[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static char[] insert(char[] src, int srcOff, char[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static boolean[] insert(boolean[] src, int srcOff, boolean[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException
If the length
argument is 0, then this method returns the dst
reference unchanged. Otherwise this method creates a new array. In every cases,
the given arrays are never modified.
src
- Array to entirely or partially insert into dst
.
Can be null only if length
is 0.srcOff
- Index of the first element of src
to insert into dst
.dst
- Array in which to insert src
data.
Can be null only if length
is 0.dstOff
- Index of the first element in dst
where to insert src
data.
All elements of dst
whose index is equal to or greater than
dstOff
will be moved forward.length
- Number of src
elements to insert.src
and dst
. This method
returns directly dst
when length
is zero, but never return
src
.NullArgumentException
- If src
or dst
is null while length
is different than 0.IllegalArgumentException
- If length
is negative.IndexOutOfBoundsException
- If srcOff
, srcOff+length
or dstOff
is out of array bounds,public static <T> T[] append(T[] array, T element) throws NullArgumentException
ArrayList
instead.T
- The type of elements in the array.array
- The array to copy with a new element. The original array will not be modified.element
- The element to add (can be null).NullArgumentException
- If the given array is null.concatenate(Object[][])
public static int removeDuplicated(Object[] array)
LinkedHashSet
instead.
This method compares all pair of elements using the Objects.equals(Object, Object)
method - so null elements are allowed. If duplicated values are found, then only the first
occurrence is retained; the second occurrence is removed in-place. After all elements have
been compared, this method returns the number of remaining elements in the array. The free
space at the end of the array is padded with null
values.
Callers can obtain an array of appropriate length using the following idiom. Note that this idiom will create a new array only if necessary:
T[] array = ...; array = resize(array, removeDuplicated(array));
Note: This method return type is not an array in order to make obvious that the given array will be modified in-place. This behavior is different than the behavior of many other methods in this class, which do not modify the given source array.
array
- Array from which to remove duplicated elements, or null
.array
was null.public static void reverse(Object[] entries)
null
, then this method does nothing.entries
- The array in which to reverse the order of elements, or null
if none.public static void reverse(int[] values)
null
, then this method does nothing.values
- The array in which to reverse the order of elements, or null
if none.public static float[] copyAsFloats(double[] data)
float
type.data
- The array to copy, or null
.float
type, or
null
if the given array was null.public static int[] copyAsInts(double[] data)
int
type.data
- The array to copy, or null
.int
type, or
null
if the given array was null.public static <E> boolean isSorted(E[] array, Comparator<? super E> comparator, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.null
elements are considered unordered and may appear anywhere in the array;
they will be silently ignored.E
- The type of array elements.array
- The array to test for order.comparator
- The comparator to use for comparing order.strict
- true
if elements should be strictly sorted (i.e. equal
elements are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static <E extends Comparable<? super E>> boolean isSorted(E[] array, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.null
elements are considered unordered and may appear anywhere in the array;
they will be silently ignored.E
- The type of array elements.array
- The array to test for order.strict
- true
if elements should be strictly sorted (i.e. equal
elements are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static boolean isSorted(double[] array, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.NaN
elements are considered unordered and may appear anywhere
in the array; they will be silently ignored.array
- The array to test for order.strict
- true
if elements should be strictly sorted (i.e. equal elements
are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static boolean isSorted(float[] array, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.NaN
elements are considered unordered and may appear anywhere
in the array; they will be silently ignored.array
- The array to test for order.strict
- true
if elements should be strictly sorted (i.e. equal elements
are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static boolean isSorted(long[] array, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.array
- The array to test for order.strict
- true
if elements should be strictly sorted (i.e. equal elements
are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static boolean isSorted(int[] array, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.array
- The array to test for order.strict
- true
if elements should be strictly sorted (i.e. equal elements
are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static boolean isSorted(short[] array, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.array
- The array to test for order.strict
- true
if elements should be strictly sorted (i.e. equal elements
are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static boolean isSorted(byte[] array, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.array
- The array to test for order.strict
- true
if elements should be strictly sorted (i.e. equal elements
are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static boolean isSorted(char[] array, boolean strict)
true
if all elements in the specified array are in increasing order.
Special cases:
NullPointerException
to be thrown.array
- The array to test for order.strict
- true
if elements should be strictly sorted (i.e. equal elements
are not allowed), or false
otherwise.true
if all elements in the given array are sorted in increasing order.public static boolean allEquals(double[] array, double value)
true
if all values in the specified array are equal to the specified
value, which may be Double.NaN
.array
- The array to check.value
- The expected value.true
if all elements in the given array are equal to the given value.public static boolean allEquals(float[] array, float value)
true
if all values in the specified array are equal to the specified
value, which may be Float.NaN
.array
- The array to check.value
- The expected value.true
if all elements in the given array are equal to the given value.public static boolean hasNaN(double[] array)
true
if the specified array contains at least one
NaN
value.array
- The array to check, or null
.true
if the given array is non-null and contains at least one NaN value.public static boolean hasNaN(float[] array)
true
if the specified array contains at least one
NaN
value.array
- The array to check, or null
.true
if the given array is non-null and contains at least one NaN value.public static boolean containsIgnoreCase(String[] array, String value)
true
if the specified array contains the specified value, ignoring case.
This method should be used only for very small arrays.array
- The array to search in. May be null
.value
- The value to search.true
if the array is non-null and contains the given value,
or false
otherwise.public static boolean containsIdentity(Object[] array, Object value)
true
if the specified array contains the specified reference.
The comparisons are performed using the ==
operator.
This method should be used only for very small arrays, or for searches to be performed
only once, because it performs a linear search. If more than one search need to be done
on the same array, consider using IdentityHashMap
instead.
array
- The array to search in. May be null
and may contains null elements.value
- The value to search. May be null
.true
if the array is non-null and contains the value (which may be null),
or false
otherwise.public static boolean contains(Object[] array, Object value)
true
if the specified array contains the specified value.
The comparisons are performed using the Object.equals(Object)
method.
This method should be used only for very small arrays, or for searches to be performed
only once, because it performs a linear search. If more than one search need to be done
on the same array, consider using HashSet
instead.
array
- The array to search in. May be null
and may contains null elements.value
- The value to search. May be null
.true
if the array is non-null and contains the value (which may be null),
or false
otherwise.intersects(Object[], Object[])
public static boolean intersects(Object[] array1, Object[] array2)
true
if at least one element in the first array is equals to an element in the second array. The element doesn't need to be at the same index
in both array.
This method should be used only for very small arrays since it may be very slow. If the
arrays are large or if an array will be involved in more than one search, consider using
HashSet
instead.
array1
- The first array, or null
.array2
- The second array, or null
.true
if both array are non-null and have at least one element in common.contains(Object[], Object)
public static <T> T[] concatenate(T[]... arrays)
arrays
argument is null
or contains only null
elements, then this method returns null
.arrays
argument contains exactly one non-null array with
a length greater than zero, then that array is returned. It is not copied.T
- The type of arrays.arrays
- The arrays to concatenate, or null
.null
.append(Object[], Object)
,
unionOfSorted(int[], int[])
public static int[] unionOfSorted(int[] array1, int[] array2)
isSorted(int[], boolean)
and Arrays.toString(int[])
methods:
assert isSorted(array1, true) : toString(array1); assert isSorted(array2, true) : toString(array2);
array1
- The first array, or null
.array2
- The second array, or null
.null
if the two
given arrays were null. May be one of the given arrays.concatenate(Object[][])
Copyright © 2010–2013 The Apache Software Foundation. All rights reserved.