Arrays
utility class.
Some worthy methods are:
- The
resize
methods, which are very similar to theArrays.copyOf(…)
methods except that they acceptnull
arrays and do not copy anything if the given array already has the requested length. - The
insert
andremove
methods for adding and removing elements in the middle of an array. - The
isSorted
methods for verifying if an array is sorted, strictly or not.
Handling of null values
Many (but not all) methods in this class are tolerant to null parameter values, sometimes under certain conditions. See the method javadoc for details.All methods in this class are tolerant to null elements in arrays. Null and NaN elements are ignored.
Performance consideration
The methods listed below are provided as convenience for casual use on small arrays. For large arrays or for frequent use, consider using the Java collection framework instead.Method | Alternative |
---|---|
resize(Object[], int) | ArrayList |
append(Object[], Object) | ArrayList |
insert(Object[], int, Object[], int, int) | LinkedList |
remove(Object[], int, int) | LinkedList |
intersects(Object[], Object[]) | HashSet |
contains(Object[], Object) | HashSet |
containsIdentity(Object[], Object) | IdentityHashMap |
- Since:
- 0.3
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final boolean[]
An empty array ofboolean
primitive type.static final byte[]
An empty array ofbyte
primitive type.static final char[]
An empty array ofchar
primitive type.static final double[]
An empty array ofdouble
primitive type.static final float[]
An empty array offloat
primitive type.static final int[]
An empty array ofint
primitive type.static final long[]
An empty array oflong
primitive type.static final short[]
An empty array ofshort
primitive type. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
allEquals
(double[] array, double value) Returnstrue
if all values in the specified array are equal to the specified value, which may beDouble.NaN
.static boolean
allEquals
(float[] array, float value) Returnstrue
if all values in the specified array are equal to the specified value, which may beFloat.NaN
.static boolean
allEquals
(int[] array, int value) Returnstrue
if all values in the specified array are equal to the specified value.static boolean
allEquals
(Object[] array, Object value) Returnstrue
if all values in the specified array are equal to the specified value, which may benull
.static <T> T[]
append
(T[] array, T element) Returns a copy of the given array with a single element appended at the end.static int[]
concatenate
(int[] a1, int[] a2) Returns the concatenation of the given arrays.static long[]
concatenate
(long[] a1, long[] a2) Returns the concatenation of the given arrays.static <T> T[]
concatenate
(T[]... arrays) Returns the concatenation of all given arrays.static boolean
Returnstrue
if the specified array contains the specified value.static boolean
containsIdentity
(Object[] array, Object value) Returnstrue
if the specified array contains the specified reference.static boolean
containsIgnoreCase
(String[] array, String value) Returnstrue
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 thefloat
type.static float[]
copyAsFloatsIfLossless
(double[] data) Returns a copy of the given array where each value has been casted to thefloat
type, but only if all casts are lossless.static long[]
copyAsLongs
(int[] data) Returns a copy of the given array where each value has been casted to thelong
type.static boolean
hasNaN
(double[] array) Returnstrue
if the specified array contains at least oneNaN
value.static boolean
hasNaN
(float[] array) Returnstrue
if the specified array contains at least oneNaN
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 another 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 another 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 another 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 another 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 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 another 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 another 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 another 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 another 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 <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 another array inserted at the given index.static boolean
intersects
(Object[] array1, Object[] array2) Returnstrue
if at least one element in the first array is equals to an element in the second array.static boolean
isRange
(int start, int[] array) Returnstrue
if the given array is a finite arithmetic progression starting at the given value and having a common difference of 1.static boolean
isSinglePrecision
(double... values) Returnstrue
if every values in the givendouble
array could be casted to thefloat
type without data lost.static boolean
isSorted
(byte[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static boolean
isSorted
(char[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static boolean
isSorted
(double[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static boolean
isSorted
(float[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static boolean
isSorted
(int[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static boolean
isSorted
(long[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static boolean
isSorted
(short[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static <E extends Comparable<? super E>>
booleanisSorted
(E[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static <E> boolean
isSorted
(E[] array, Comparator<? super E> comparator, boolean strict) Returnstrue
if all elements in the specified array are in increasing order.static int[]
range
(int start, int end) Returns the ordered values in the range fromstart
inclusive toend
exclusive.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 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 <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 int
removeDuplicated
(Object[] array) Removes the duplicated elements in the given array.static void
replace
(double[] array, double search, double replacement) Replaces all occurrences of the given value by the given replacement.static void
replace
(float[] array, float search, float replacement) Replaces all occurrences of the given value by the given replacement.static boolean[]
resize
(boolean[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding withfalse
if necessary.static byte[]
resize
(byte[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary.static char[]
resize
(char[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary.static double[]
resize
(double[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary.static float[]
resize
(float[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary.static int[]
resize
(int[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary.static long[]
resize
(long[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary.static short[]
resize
(short[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary.static <E> E[]
resize
(E[] array, int length) Returns an array containing the same elements as the givenarray
but with the specifiedlength
, truncating or padding withnull
if necessary.static void
reverse
(int[] values) Reverses the order of elements in the given array.static void
Reverses the order of elements in the given array.static void
swap
(byte[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofbyte
values.static void
swap
(char[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofchar
values.static void
swap
(double[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofdouble
values.static void
swap
(float[] data, int i0, int i1) Swaps the elements at the given indices in the given array offloat
values.static void
swap
(int[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofint
values.static void
swap
(long[] data, int i0, int i1) Swaps the elements at the given indices in the given array oflong
values.static void
swap
(short[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofshort
values.static void
Swaps the elements at the given indices in the given array ofObject
values.static int[]
unionOfSorted
(int[] array1, int[] array2) Returns the union of two sorted arrays.
-
Field Details
-
EMPTY_DOUBLE
public static final double[] EMPTY_DOUBLEAn empty array ofdouble
primitive type. Such arrays are immutable and can be safely shared. -
EMPTY_FLOAT
public static final float[] EMPTY_FLOATAn empty array offloat
primitive type. Such arrays are immutable and can be safely shared. -
EMPTY_LONG
public static final long[] EMPTY_LONGAn empty array oflong
primitive type. Such arrays are immutable and can be safely shared. -
EMPTY_INT
public static final int[] EMPTY_INTAn empty array ofint
primitive type. Such arrays are immutable and can be safely shared. -
EMPTY_SHORT
public static final short[] EMPTY_SHORTAn empty array ofshort
primitive type. Such arrays are immutable and can be safely shared. -
EMPTY_BYTE
public static final byte[] EMPTY_BYTEAn empty array ofbyte
primitive type. Such arrays are immutable and can be safely shared. -
EMPTY_CHAR
public static final char[] EMPTY_CHARAn empty array ofchar
primitive type. Such arrays are immutable and can be safely shared. -
EMPTY_BOOLEAN
public static final boolean[] EMPTY_BOOLEANAn empty array ofboolean
primitive type. Such arrays are immutable and can be safely shared.
-
-
Method Details
-
resize
public static <E> E[] resize(E[] array, int length) Returns an array containing the same elements as the givenarray
but with the specifiedlength
, truncating or padding withnull
if necessary.- If the given
length
is longer than the length of the givenarray
, then the returned array will contain all the elements ofarray
at index i <array.length
. Elements at index i >=array.length
are initialized tonull
. - If the given
length
is shorter than the length of the givenarray
, then the returned array will contain only the elements ofarray
at index i <length
. Remaining elements are not copied. - If the given
length
is equal to the length of the givenarray
, thenarray
is returned unchanged. No copy is performed. This behavior is different than theArrays.copyOf(Object[], int)
one.
null
, then this method unconditionally returnsnull
no matter the value of thelength
argument.- Type Parameters:
E
- the array elements.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
- If the given
-
resize
public static double[] resize(double[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary. This method returnsnull
if and only if the given array isnull
, in which case the value of thelength
argument is ignored.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
-
resize
public static float[] resize(float[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary. This method returnsnull
if and only if the given array isnull
, in which case the value of thelength
argument is ignored.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
-
resize
public static long[] resize(long[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary. This method returnsnull
if and only if the given array isnull
, in which case the value of thelength
argument is ignored.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
-
resize
public static int[] resize(int[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary. This method returnsnull
if and only if the given array isnull
, in which case the value of thelength
argument is ignored.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
-
resize
public static short[] resize(short[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary. This method returnsnull
if and only if the given array isnull
, in which case the value of thelength
argument is ignored.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
-
resize
public static byte[] resize(byte[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary. This method returnsnull
if and only if the given array isnull
, in which case the value of thelength
argument is ignored.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
-
resize
public static char[] resize(char[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding with zeros if necessary. This method returnsnull
if and only if the given array isnull
, in which case the value of thelength
argument is ignored.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
-
resize
public static boolean[] resize(boolean[] array, int length) Returns an array containing the same elements as the givenarray
but specifiedlength
, truncating or padding withfalse
if necessary. This method returnsnull
if and only if the given array isnull
, in which case the value of thelength
argument is ignored.- Parameters:
array
- array to resize, ornull
.length
- length of the desired array.- Returns:
- a new array of the requested length, or
array
if the given array isnull
or already have the requested length. - Throws:
NegativeArraySizeException
- iflength
is negative.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Type Parameters:
E
- the type of array elements.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged, except for empty arrays. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged, except for empty arrays. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged, except for empty arrays. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged, except for empty arrays. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged, except for empty arrays. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged, except for empty arrays. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged, except for empty arrays. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
remove
public 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. If thelength
argument is 0, then this method returns thearray
reference unchanged, except for empty arrays. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array from which to remove elements. Can benull
only iflength
is 0.first
- index of the first element to remove from the givenarray
.length
- number of elements to remove.- Returns:
- array with the same elements than the given
array
except for the removed elements, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up ofnull
elements. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Type Parameters:
E
- the type of array elements.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space will be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward. Can bearray.length
for inserting spaces at the end of the array.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up of elements initialized to zero. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space will be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward. Can bearray.length
for inserting spaces at the end of the array.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up of elements initialized to zero. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space will be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward. Can bearray.length
for inserting spaces at the end of the array.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up of elements initialized to zero. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space will be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward. Can bearray.length
for inserting spaces at the end of the array.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up of elements initialized to zero. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space will be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward. Can bearray.length
for inserting spaces at the end of the array.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up of elements initialized to zero. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space should be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up of elements initialized to zero. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space will be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward. Can bearray.length
for inserting spaces at the end of the array.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up of elements initialized to zero. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space will be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward. Can bearray.length
for inserting spaces at the end of the array.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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. These "spaces" will be made up of elements initialized tofalse
. If thelength
argument is 0, then this method returns thearray
reference unchanged. Otherwise this method creates a new array. In every cases, the given array is never modified.- Parameters:
array
- array in which to insert spaces. Can benull
only iflength
is 0.first
- index where the first space will be inserted. Allarray
elements having an index equal to or higher thanindex
will be moved forward. Can bearray.length
for inserting spaces at the end of the array.length
- number of spaces to insert.- Returns:
- array containing the
array
elements with the additional space inserted, orarray
(which may be null) iflength
is 0. - Throws:
NullPointerException
- ifarray
is null andlength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- iffirst
orfirst+length
is out of array bounds.- See Also:
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Type Parameters:
E
- the type of array elements.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
insert
public 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 another array inserted at the given index. If thelength
argument is 0, then this method returns thedst
reference unchanged. Otherwise this method creates a new array. In every cases, the given arrays are never modified.- Parameters:
src
- array to entirely or partially insert intodst
. Can be null only iflength
is 0.srcOff
- index of the first element ofsrc
to insert intodst
.dst
- array in which to insertsrc
data. Can be null only iflength
is 0.dstOff
- index of the first element indst
where to insertsrc
data. All elements ofdst
whose index is equal to or greater thandstOff
will be moved forward.length
- number ofsrc
elements to insert.- Returns:
- array which contains the merge of
src
anddst
. This method returns directlydst
whenlength
is zero, but never returnsrc
. - Throws:
NullPointerException
- ifsrc
ordst
is null whilelength
is different than 0.IllegalArgumentException
- iflength
is negative.IndexOutOfBoundsException
- ifsrcOff
,srcOff+length
ordstOff
is out of array bounds.
-
append
public static <T> T[] append(T[] array, T element) Returns a copy of the given array with a single element appended at the end. This method should be invoked only on rare occasions. If many elements are to be added, useArrayList
instead.- Type Parameters:
T
- the type of elements in the array.- Parameters:
array
- the array to copy with a new element. The original array will not be modified.element
- the element to add (can be null).- Returns:
- a copy of the given array with the given element appended at the end.
- Throws:
NullPointerException
- if the given array is null.- See Also:
-
concatenate
Returns the concatenation of all given arrays. This method performs the following checks:- If the
arrays
argument isnull
or contains onlynull
elements, then this method returnsnull
. - Otherwise if the
arrays
argument contains exactly one non-null array with a length greater than zero, then that array is returned. It is not copied. - Otherwise a new array with a length equals to the sum of the length of every non-null arrays is created, and the content of non-null arrays are appended in the new array in declaration order.
- Type Parameters:
T
- the type of arrays.- Parameters:
arrays
- the arrays to concatenate, ornull
.- Returns:
- the concatenation of all non-null arrays (may be a direct reference to one
of the given array if it can be returned with no change), or
null
. - See Also:
- If the
-
concatenate
public static long[] concatenate(long[] a1, long[] a2) Returns the concatenation of the given arrays. If any of the supplied arrays is null or empty, then the other array is returned directly (not copied).- Parameters:
a1
- the first array to concatenate, ornull
.a2
- the second array to concatenate, ornull
.- Returns:
- the concatenation of given arrays. May be one of the given arrays returned without copying.
- Since:
- 1.4
-
concatenate
public static int[] concatenate(int[] a1, int[] a2) Returns the concatenation of the given arrays. If any of the supplied arrays is null or empty, then the other array is returned directly (not copied).- Parameters:
a1
- the first array to concatenate, ornull
.a2
- the second array to concatenate, ornull
.- Returns:
- the concatenation of given arrays. May be one of the given arrays returned without copying.
- Since:
- 1.4
-
removeDuplicated
Removes the duplicated elements in the given array. This method should be invoked only for small arrays, typically less than 10 distinct elements. For larger arrays, useLinkedHashSet
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 withnull
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));
API 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.- Parameters:
array
- array from which to remove duplicated elements, ornull
.- Returns:
- the number of remaining elements in the given array, or 0 if the given
array
was null.
-
reverse
Reverses the order of elements in the given array. This operation is performed in-place. If the given array isnull
, then this method does nothing.- Parameters:
entries
- the array in which to reverse the order of elements, ornull
if none.
-
reverse
public static void reverse(int[] values) Reverses the order of elements in the given array. This operation is performed in-place. If the given array isnull
, then this method does nothing.- Parameters:
values
- the array in which to reverse the order of elements, ornull
if none.
-
range
public static int[] range(int start, int end) Returns the ordered values in the range fromstart
inclusive toend
exclusive. This method performs the same work thanIntStream.range(int, int)
but returning values in an array instead of in a stream. This method is okay for small sequences; for large sequences the stream approach should be preferred.For any array returned by this method,
is guaranteed to returnisRange
(start, array)true
.Use case
This method is convenient for enumerating dimensions in a coordinate reference system or bands in an image. Some methods in the Java library or in Apache SIS want dimensions or bands to be specified by their indices. An example from the Java library is thebankIndices
argument inRaster.createBandedRaster(…, bankIndices, …)
. An example from Apache SIS is therange
argument inGridCoverageResource.read(…, range)
. Thisrange(start, end)
method can be used in the common case where all bands are wanted in order.- Parameters:
start
- first value (inclusive) in the array to return.end
- upper bound (exclusive) of values in the array to return.- Returns:
- a finite arithmetic progression of common difference of 1 with all values in the specified range.
- Throws:
ArithmeticException
- if the sequence length is greater thanInteger.MAX_VALUE
.- Since:
- 1.0
- See Also:
-
isRange
public static boolean isRange(int start, int[] array) Returnstrue
if the given array is a finite arithmetic progression starting at the given value and having a common difference of 1. More specifically:- If
array
isnull
, then returnfalse
. - Otherwise if
array
is empty, then returntrue
for consistency withrange(int, int)
. - Otherwise for any index 0 ≤ i <
array.length
, ifarray[i]
is equal tostart + i
(computed as if no overflow occurs), then returntrue
. - Otherwise return
false
.
array
is an argument specified to another method, and determining that the argument values arestart
,start
+1,start
+2, etc. allows some optimizations.Example
isRange(1, array)
returnstrue
if the given array is{1, 2, 3, 4}
butfalse
if the array is{1, 2, 4}
(missing 3).- Parameters:
start
- first value expected in the givenarray
.array
- the array to test, ornull
.- Returns:
true
if the given array is non-null and equal torange(start, start + array.length)
.- Since:
- 1.0
- See Also:
- If
-
isSorted
Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown. null
elements are considered unordered and may appear anywhere in the array; they will be silently ignored.
- Type Parameters:
E
- the type of array elements.- Parameters:
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), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
isSorted
Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown. null
elements are considered unordered and may appear anywhere in the array; they will be silently ignored.
- Type Parameters:
E
- the type of array elements.- Parameters:
array
- the array to test for order.strict
-true
if elements should be strictly sorted (i.e. equal elements are not allowed), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(double[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown. NaN
elements are considered unordered and may appear anywhere in the array; they will be silently ignored.
- Parameters:
array
- the array to test for order.strict
-true
if elements should be strictly sorted (i.e. equal elements are not allowed), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(float[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown. NaN
elements are considered unordered and may appear anywhere in the array; they will be silently ignored.
- Parameters:
array
- the array to test for order.strict
-true
if elements should be strictly sorted (i.e. equal elements are not allowed), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(long[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown.
- Parameters:
array
- the array to test for order.strict
-true
if elements should be strictly sorted (i.e. equal elements are not allowed), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(int[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown.
- Parameters:
array
- the array to test for order.strict
-true
if elements should be strictly sorted (i.e. equal elements are not allowed), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(short[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown.
- Parameters:
array
- the array to test for order.strict
-true
if elements should be strictly sorted (i.e. equal elements are not allowed), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(byte[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown.
- Parameters:
array
- the array to test for order.strict
-true
if elements should be strictly sorted (i.e. equal elements are not allowed), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(char[] array, boolean strict) Returnstrue
if all elements in the specified array are in increasing order. Special cases:- Empty arrays are considered as sorted.
- Null arrays are considered as unknown content and cause a
NullPointerException
to be thrown.
- Parameters:
array
- the array to test for order.strict
-true
if elements should be strictly sorted (i.e. equal elements are not allowed), orfalse
otherwise.- Returns:
true
if all elements in the given array are sorted in increasing order.
-
swap
Swaps the elements at the given indices in the given array ofObject
values.Note: While trivial, this method is provided because its need occurs relatively often and the availability of aswap
method makes the code easier to read.- Parameters:
data
- the array in which to swap elements.i0
- index of one element to be swapped.i1
- index of the other element to be swapped.- Since:
- 0.4
-
swap
public static void swap(double[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofdouble
values.Note: While trivial, this method is provided because its need occurs relatively often and the availability of aswap
method makes the code easier to read.- Parameters:
data
- the array in which to swap elements.i0
- index of one element to be swapped.i1
- index of the other element to be swapped.- Since:
- 0.4
-
swap
public static void swap(float[] data, int i0, int i1) Swaps the elements at the given indices in the given array offloat
values.- Parameters:
data
- the array in which to swap elements.i0
- index of one element to be swapped.i1
- index of the other element to be swapped.- Since:
- 0.4
-
swap
public static void swap(long[] data, int i0, int i1) Swaps the elements at the given indices in the given array oflong
values.- Parameters:
data
- the array in which to swap elements.i0
- index of one element to be swapped.i1
- index of the other element to be swapped.- Since:
- 0.4
-
swap
public static void swap(int[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofint
values.Note: While trivial, this method is provided because its need occurs relatively often and the availability of aswap
method makes the code easier to read.- Parameters:
data
- the array in which to swap elements.i0
- index of one element to be swapped.i1
- index of the other element to be swapped.- Since:
- 0.4
-
swap
public static void swap(short[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofshort
values.- Parameters:
data
- the array in which to swap elements.i0
- index of one element to be swapped.i1
- index of the other element to be swapped.- Since:
- 0.4
-
swap
public static void swap(byte[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofbyte
values.- Parameters:
data
- the array in which to swap elements.i0
- index of one element to be swapped.i1
- index of the other element to be swapped.- Since:
- 0.4
-
swap
public static void swap(char[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofchar
values.- Parameters:
data
- the array in which to swap elements.i0
- index of one element to be swapped.i1
- index of the other element to be swapped.- Since:
- 0.4
-
replace
public static void replace(double[] array, double search, double replacement) Replaces all occurrences of the given value by the given replacement. This method compares the values usingDouble.doubleToRawLongBits(double)
:- Positive zero is considered different then negative zero.
- The various possible NaN values are considered different.
Double.NaN
in the sample values of a grid coverage. This method does nothing if the given array isnull
or ifsearch
is the same bits pattern thanreplacement
.- Parameters:
array
- the array where to perform the search and replace, ornull
.search
- the value to search.replacement
- the replacement.- Since:
- 1.0
-
replace
public static void replace(float[] array, float search, float replacement) Replaces all occurrences of the given value by the given replacement. This method compares the values usingFloat.floatToRawIntBits(float)
:- Positive zero is considered different then negative zero.
- The various possible NaN values are considered different.
Float.NaN
in the sample values of a grid coverage. This method does nothing if the given array isnull
or ifsearch
is the same bits pattern thanreplacement
.- Parameters:
array
- the array where to perform the search and replace, ornull
.search
- the value to search.replacement
- the replacement.- Since:
- 1.0
-
copyAsLongs
public static long[] copyAsLongs(int[] data) Returns a copy of the given array where each value has been casted to thelong
type.- Parameters:
data
- the array to copy, ornull
.- Returns:
- a copy of the given array with values casted to the
long
type, ornull
if the given array was null. - Since:
- 1.1
-
copyAsFloats
public static float[] copyAsFloats(double[] data) Returns a copy of the given array where each value has been casted to thefloat
type. This method does not verify if the casts would cause data loss.- Parameters:
data
- the array to copy, ornull
.- Returns:
- a copy of the given array with values casted to the
float
type, ornull
if the given array was null. - Since:
- 1.0
-
copyAsFloatsIfLossless
public static float[] copyAsFloatsIfLossless(double[] data) Returns a copy of the given array where each value has been casted to thefloat
type, but only if all casts are lossless. If any cast causes data loss, then this method returnsnull
. This method is equivalent to the following code, but potentially more efficient:if (isSinglePrecision(data)) { return copyAsFloat(data); } else { return null; }
- Parameters:
data
- the array to copy, ornull
.- Returns:
- a copy of the given array with values casted to the
float
type, ornull
if the given array was null or if a cast would cause data lost. - Since:
- 1.0
-
isSinglePrecision
public static boolean isSinglePrecision(double... values) Returnstrue
if every values in the givendouble
array could be casted to thefloat
type without data lost. If this method returnstrue
, then the array can be converted to thefloat[]
type withcopyAsFloats(double[])
and the exact samedouble
values can still be obtained by casting back eachfloat
value todouble
.- Parameters:
values
- the values to test for their precision, ornull
.- Returns:
true
if every values can be casted to thefloat
type without data lost.- Since:
- 1.0
-
hasNaN
public static boolean hasNaN(double[] array) Returnstrue
if the specified array contains at least oneNaN
value.- Parameters:
array
- the array to check, ornull
.- Returns:
true
if the given array is non-null and contains at least one NaN value.
-
hasNaN
public static boolean hasNaN(float[] array) Returnstrue
if the specified array contains at least oneNaN
value.- Parameters:
array
- the array to check, ornull
.- Returns:
true
if the given array is non-null and contains at least one NaN value.
-
allEquals
Returnstrue
if all values in the specified array are equal to the specified value, which may benull
. If the given array is empty, then this method returnstrue
.- Parameters:
array
- the array to check.value
- the expected value.- Returns:
true
if all elements in the given array are equal to the given value.- Since:
- 0.8
-
allEquals
public static boolean allEquals(double[] array, double value) Returnstrue
if all values in the specified array are equal to the specified value, which may beDouble.NaN
. A NaN value is considered equal to all other NaN values.- Parameters:
array
- the array to check.value
- the expected value.- Returns:
true
if all elements in the given array are equal to the given value.
-
allEquals
public static boolean allEquals(float[] array, float value) Returnstrue
if all values in the specified array are equal to the specified value, which may beFloat.NaN
.- Parameters:
array
- the array to check.value
- the expected value.- Returns:
true
if all elements in the given array are equal to the given value.
-
allEquals
public static boolean allEquals(int[] array, int value) Returnstrue
if all values in the specified array are equal to the specified value.- Parameters:
array
- the array to check.value
- the expected value.- Returns:
true
if all elements in the given array are equal to the given value.- Since:
- 1.2
-
containsIgnoreCase
Returnstrue
if the specified array contains the specified value, ignoring case. This method should be used only for very small arrays.- Parameters:
array
- the array to search in. May benull
.value
- the value to search.- Returns:
true
if the array is non-null and contains the given value, orfalse
otherwise.
-
containsIdentity
Returnstrue
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.- Parameters:
array
- the array to search in. May benull
and may contains null elements.value
- the value to search. May benull
.- Returns:
true
if the array is non-null and contains the value (which may be null), orfalse
otherwise.
-
contains
Returnstrue
if the specified array contains the specified value. The comparisons are performed using theObject.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.- Parameters:
array
- the array to search in. May benull
and may contains null elements.value
- the value to search. May benull
.- Returns:
true
if the array is non-null and contains the value (which may be null), orfalse
otherwise.- See Also:
-
intersects
Returnstrue
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.- Parameters:
array1
- the first array, ornull
.array2
- the second array, ornull
.- Returns:
true
if both array are non-null and have at least one element in common.- See Also:
-
unionOfSorted
public static int[] unionOfSorted(int[] array1, int[] array2) Returns the union of two sorted arrays. The input arrays shall be sorted in strictly increasing order. The output array is the union of the input arrays without duplicated values, with elements sorted in strictly increasing order.Recommended assertions
Callers are encouraged to place the following assertions before calls to this method, using theisSorted(int[], boolean)
andArrays.toString(int[])
methods:assert isSorted(array1, true) : toString(array1); assert isSorted(array2, true) : toString(array2);
- Parameters:
array1
- the first array, ornull
.array2
- the second array, ornull
.- Returns:
- the union of the given array without duplicated values, or
null
if the two given arrays were null. May be one of the given arrays. - See Also:
-