Class ArraysExt
Arrays utility class.
Some worthy methods are:
- The
resizemethods, which are very similar to theArrays.copyOf(…)methods except that they acceptnullarrays and do not copy anything if the given array already has the requested length. - The
insertandremovemethods for adding and removing elements in the middle of an array. - The
isSortedmethods 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:
Defined in the sis-utility module
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final boolean[]An empty array ofbooleanprimitive type.static final byte[]An empty array ofbyteprimitive type.static final char[]An empty array ofcharprimitive type.static final double[]An empty array ofdoubleprimitive type.static final float[]An empty array offloatprimitive type.static final int[]An empty array ofintprimitive type.static final long[]An empty array oflongprimitive type.static final short[]An empty array ofshortprimitive type. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanallEquals(double[] array, double value) Returnstrueif all values in the specified array are equal to the specified value, which may beDouble.NaN.static booleanallEquals(float[] array, float value) Returnstrueif all values in the specified array are equal to the specified value, which may beFloat.NaN.static booleanallEquals(int[] array, int value) Returnstrueif all values in the specified array are equal to the specified value.static booleanallEquals(Object[] array, Object value) Returnstrueif 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 <T> T[]concatenate(T[]... arrays) Returns the concatenation of all given arrays.static booleanReturnstrueif the specified array contains the specified value.static booleancontainsIdentity(Object[] array, Object value) Returnstrueif the specified array contains the specified reference.static booleancontainsIgnoreCase(String[] array, String value) Returnstrueif 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 thefloattype.static float[]copyAsFloatsIfLossless(double[] data) Returns a copy of the given array where each value has been casted to thefloattype, 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 thelongtype.static booleanhasNaN(double[] array) Returnstrueif the specified array contains at least oneNaNvalue.static booleanhasNaN(float[] array) Returnstrueif the specified array contains at least oneNaNvalue.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 booleanintersects(Object[] array1, Object[] array2) Returnstrueif at least one element in the first array is equals to an element in the second array.static booleanisRange(int start, int[] array) Returnstrueif the given array is a finite arithmetic progression starting at the given value and having a common difference of 1.static booleanisSinglePrecision(double... values) Returnstrueif every values in the givendoublearray could be casted to thefloattype without data lost.static booleanisSorted(byte[] array, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static booleanisSorted(char[] array, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static booleanisSorted(double[] array, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static booleanisSorted(float[] array, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static booleanisSorted(int[] array, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static booleanisSorted(long[] array, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static booleanisSorted(short[] array, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static <E extends Comparable<? super E>>
booleanisSorted(E[] array, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static <E> booleanisSorted(E[] array, Comparator<? super E> comparator, boolean strict) Returnstrueif all elements in the specified array are in increasing order.static int[]range(int start, int end) Returns the ordered values in the range fromstartinclusive toendexclusive.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 intremoveDuplicated(Object[] array) Removes the duplicated elements in the given array.static voidreplace(double[] array, double search, double replacement) Replaces all occurrences of the given value by the given replacement.static voidreplace(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 givenarraybut specifiedlength, truncating or padding withfalseif necessary.static byte[]resize(byte[] array, int length) Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary.static char[]resize(char[] array, int length) Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary.static double[]resize(double[] array, int length) Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary.static float[]resize(float[] array, int length) Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary.static int[]resize(int[] array, int length) Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary.static long[]resize(long[] array, int length) Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary.static short[]resize(short[] array, int length) Returns an array containing the same elements as the givenarraybut 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 givenarraybut with the specifiedlength, truncating or padding withnullif necessary.static voidreverse(int[] values) Reverses the order of elements in the given array.static voidReverses the order of elements in the given array.static voidswap(byte[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofbytevalues.static voidswap(char[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofcharvalues.static voidswap(double[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofdoublevalues.static voidswap(float[] data, int i0, int i1) Swaps the elements at the given indices in the given array offloatvalues.static voidswap(int[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofintvalues.static voidswap(long[] data, int i0, int i1) Swaps the elements at the given indices in the given array oflongvalues.static voidswap(short[] data, int i0, int i1) Swaps the elements at the given indices in the given array ofshortvalues.static voidSwaps the elements at the given indices in the given array ofObjectvalues.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 ofdoubleprimitive type. Such arrays are immutable and can be safely shared. -
EMPTY_FLOAT
public static final float[] EMPTY_FLOATAn empty array offloatprimitive type. Such arrays are immutable and can be safely shared. -
EMPTY_LONG
public static final long[] EMPTY_LONGAn empty array oflongprimitive type. Such arrays are immutable and can be safely shared. -
EMPTY_INT
public static final int[] EMPTY_INTAn empty array ofintprimitive type. Such arrays are immutable and can be safely shared. -
EMPTY_SHORT
public static final short[] EMPTY_SHORTAn empty array ofshortprimitive type. Such arrays are immutable and can be safely shared. -
EMPTY_BYTE
public static final byte[] EMPTY_BYTEAn empty array ofbyteprimitive type. Such arrays are immutable and can be safely shared. -
EMPTY_CHAR
public static final char[] EMPTY_CHARAn empty array ofcharprimitive type. Such arrays are immutable and can be safely shared. -
EMPTY_BOOLEAN
public static final boolean[] EMPTY_BOOLEANAn empty array ofbooleanprimitive type. Such arrays are immutable and can be safely shared.
-
-
Method Details
-
resize
Returns an array containing the same elements as the givenarraybut with the specifiedlength, truncating or padding withnullif necessary.- If the given
lengthis longer than the length of the givenarray, then the returned array will contain all the elements ofarrayat index i <array.length. Elements at index i >=array.lengthare initialized tonull. - If the given
lengthis shorter than the length of the givenarray, then the returned array will contain only the elements ofarrayat index i <length. Remaining elements are not copied. - If the given
lengthis equal to the length of the givenarray, thenarrayis returned unchanged. No copy is performed. This behavior is different than theArrays.copyOf(Object[], int)one.
null, then this method unconditionally returnsnullno matter the value of thelengthargument.- 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
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
- If the given
-
resize
Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary. This method returnsnullif and only if the given array isnull, in which case the value of thelengthargument is ignored.- Parameters:
array- array to resize, ornull.length- length of the desired array.- Returns:
- a new array of the requested length, or
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
-
resize
Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary. This method returnsnullif and only if the given array isnull, in which case the value of thelengthargument is ignored.- Parameters:
array- array to resize, ornull.length- length of the desired array.- Returns:
- a new array of the requested length, or
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
-
resize
Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary. This method returnsnullif and only if the given array isnull, in which case the value of thelengthargument is ignored.- Parameters:
array- array to resize, ornull.length- length of the desired array.- Returns:
- a new array of the requested length, or
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
-
resize
Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary. This method returnsnullif and only if the given array isnull, in which case the value of thelengthargument is ignored.- Parameters:
array- array to resize, ornull.length- length of the desired array.- Returns:
- a new array of the requested length, or
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
-
resize
Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary. This method returnsnullif and only if the given array isnull, in which case the value of thelengthargument is ignored.- Parameters:
array- array to resize, ornull.length- length of the desired array.- Returns:
- a new array of the requested length, or
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
-
resize
Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary. This method returnsnullif and only if the given array isnull, in which case the value of thelengthargument is ignored.- Parameters:
array- array to resize, ornull.length- length of the desired array.- Returns:
- a new array of the requested length, or
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
-
resize
Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding with zeros if necessary. This method returnsnullif and only if the given array isnull, in which case the value of thelengthargument is ignored.- Parameters:
array- array to resize, ornull.length- length of the desired array.- Returns:
- a new array of the requested length, or
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
-
resize
Returns an array containing the same elements as the givenarraybut specifiedlength, truncating or padding withfalseif necessary. This method returnsnullif and only if the given array isnull, in which case the value of thelengthargument is ignored.- Parameters:
array- array to resize, ornull.length- length of the desired array.- Returns:
- a new array of the requested length, or
arrayif the given array isnullor already have the requested length. - Throws:
NegativeArraySizeException- iflengthis negative.- See Also:
-
remove
public static <E> E[] remove(E[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
remove
public static double[] remove(double[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
remove
public static float[] remove(float[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
remove
public static long[] remove(long[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
remove
public static int[] remove(int[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
remove
public static short[] remove(short[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
remove
public static byte[] remove(byte[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
remove
public static char[] remove(char[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
remove
public static boolean[] remove(boolean[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array except for the given range. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 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
arrayexcept for the removed elements, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static <E> E[] insert(E[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with additional "spaces" in the given range. These "spaces" will be made up ofnullelements. If thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space will be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward. Can bearray.lengthfor inserting spaces at the end of the array.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static double[] insert(double[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException 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 thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space will be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward. Can bearray.lengthfor inserting spaces at the end of the array.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static float[] insert(float[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException 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 thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space will be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward. Can bearray.lengthfor inserting spaces at the end of the array.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static long[] insert(long[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException 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 thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space will be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward. Can bearray.lengthfor inserting spaces at the end of the array.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static int[] insert(int[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException 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 thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space will be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward. Can bearray.lengthfor inserting spaces at the end of the array.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static short[] insert(short[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException 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 thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space should be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static byte[] insert(byte[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException 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 thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space will be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward. Can bearray.lengthfor inserting spaces at the end of the array.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static char[] insert(char[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException 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 thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space will be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward. Can bearray.lengthfor inserting spaces at the end of the array.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static boolean[] insert(boolean[] array, int first, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException 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 thelengthargument is 0, then this method returns thearrayreference 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 benullonly iflengthis 0.first- index where the first space will be inserted. Allarrayelements having an index equal to or higher thanindexwill be moved forward. Can bearray.lengthfor inserting spaces at the end of the array.length- number of spaces to insert.- Returns:
- array containing the
arrayelements with the additional space inserted, orarray(which may be null) iflengthis 0. - Throws:
NullArgumentException- ifarrayis null andlengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- iffirstorfirst+lengthis out of array bounds.- See Also:
-
insert
public static <E> E[] insert(E[] src, int srcOff, E[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
insert
public static double[] insert(double[] src, int srcOff, double[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
insert
public static float[] insert(float[] src, int srcOff, float[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
insert
public static long[] insert(long[] src, int srcOff, long[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
insert
public static int[] insert(int[] src, int srcOff, int[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
insert
public static short[] insert(short[] src, int srcOff, short[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
insert
public static byte[] insert(byte[] src, int srcOff, byte[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
insert
public static char[] insert(char[] src, int srcOff, char[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
insert
public static boolean[] insert(boolean[] src, int srcOff, boolean[] dst, int dstOff, int length) throws NullArgumentException, IllegalArgumentException, IndexOutOfBoundsException Returns an array containing the same elements than the given array, with the content of another array inserted at the given index. If thelengthargument is 0, then this method returns thedstreference 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 iflengthis 0.srcOff- index of the first element ofsrcto insert intodst.dst- array in which to insertsrcdata. Can be null only iflengthis 0.dstOff- index of the first element indstwhere to insertsrcdata. All elements ofdstwhose index is equal to or greater thandstOffwill be moved forward.length- number ofsrcelements to insert.- Returns:
- array which contains the merge of
srcanddst. This method returns directlydstwhenlengthis zero, but never returnsrc. - Throws:
NullArgumentException- ifsrcordstis null whilelengthis different than 0.IllegalArgumentException- iflengthis negative.IndexOutOfBoundsException- ifsrcOff,srcOff+lengthordstOffis out of array bounds.
-
append
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, useArrayListinstead.- 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:
NullArgumentException- if the given array is null.- See Also:
-
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, useLinkedHashSetinstead.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 withnullvalues.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
arraywas 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, ornullif 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, ornullif none.
-
range
public static int[] range(int start, int end) Returns the ordered values in the range fromstartinclusive toendexclusive. 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.Purpose: 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 theFor any array returned by this method,bankIndicesargument inRaster.createBandedRaster(…, bankIndices, …). An example from Apache SIS is therangeargument inGridCoverageResource.read(…, range). Thisrange(start, end)method can be used in the common case where all bands are wanted in order.is guaranteed to returnisRange(start, array)true.- 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) Returnstrueif the given array is a finite arithmetic progression starting at the given value and having a common difference of 1. More specifically:- If
arrayisnull, then returnfalse. - Otherwise if
arrayis empty, then returntruefor 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.
Example:This method is useful whenisRange(1, array)returnstrueif the given array is{1, 2, 3, 4}butfalseif the array is{1, 2, 4}(missing 3).arrayis an argument specified to another method, and determining that the argument values arestart,start+1,start+2, etc. allows some optimizations.- Parameters:
start- first value expected in the givenarray.array- the array to test, ornull.- Returns:
trueif the given array is non-null and equal torange(start, start + array.length).- Since:
- 1.0
- See Also:
- If
-
isSorted
Returnstrueif 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
NullPointerExceptionto be thrown. nullelements 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-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
isSorted
Returnstrueif 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
NullPointerExceptionto be thrown. nullelements 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-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(double[] array, boolean strict) Returnstrueif 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
NullPointerExceptionto be thrown. NaNelements are considered unordered and may appear anywhere in the array; they will be silently ignored.
- Parameters:
array- the array to test for order.strict-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(float[] array, boolean strict) Returnstrueif 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
NullPointerExceptionto be thrown. NaNelements are considered unordered and may appear anywhere in the array; they will be silently ignored.
- Parameters:
array- the array to test for order.strict-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(long[] array, boolean strict) Returnstrueif 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
NullPointerExceptionto be thrown.
- Parameters:
array- the array to test for order.strict-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(int[] array, boolean strict) Returnstrueif 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
NullPointerExceptionto be thrown.
- Parameters:
array- the array to test for order.strict-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(short[] array, boolean strict) Returnstrueif 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
NullPointerExceptionto be thrown.
- Parameters:
array- the array to test for order.strict-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(byte[] array, boolean strict) Returnstrueif 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
NullPointerExceptionto be thrown.
- Parameters:
array- the array to test for order.strict-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
isSorted
public static boolean isSorted(char[] array, boolean strict) Returnstrueif 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
NullPointerExceptionto be thrown.
- Parameters:
array- the array to test for order.strict-trueif elements should be strictly sorted (i.e. equal elements are not allowed), orfalseotherwise.- Returns:
trueif all elements in the given array are sorted in increasing order.
-
swap
Swaps the elements at the given indices in the given array ofObjectvalues.Note: While trivial, this method is provided because its need occurs relatively often and the availability of aswapmethod 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 ofdoublevalues.Note: While trivial, this method is provided because its need occurs relatively often and the availability of aswapmethod 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 offloatvalues.- 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 oflongvalues.- 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 ofintvalues.Note: While trivial, this method is provided because its need occurs relatively often and the availability of aswapmethod 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 ofshortvalues.- 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 ofbytevalues.- 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 ofcharvalues.- 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.NaNin the sample values of a grid coverage. This method does nothing if the given array isnullor ifsearchis 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.NaNin the sample values of a grid coverage. This method does nothing if the given array isnullor ifsearchis 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 thelongtype.- Parameters:
data- the array to copy, ornull.- Returns:
- a copy of the given array with values casted to the
longtype, ornullif 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 thefloattype. 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
floattype, ornullif 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 thefloattype, 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
floattype, ornullif the given array was null or if a cast would cause data lost. - Since:
- 1.0
-
isSinglePrecision
public static boolean isSinglePrecision(double... values) Returnstrueif every values in the givendoublearray could be casted to thefloattype without data lost. If this method returnstrue, then the array can be converted to thefloat[]type withcopyAsFloats(double[])and the exact samedoublevalues can still be obtained by casting back eachfloatvalue todouble.- Parameters:
values- the values to test for their precision, ornull.- Returns:
trueif every values can be casted to thefloattype without data lost.- Since:
- 1.0
-
hasNaN
public static boolean hasNaN(double[] array) Returnstrueif the specified array contains at least oneNaNvalue.- Parameters:
array- the array to check, ornull.- Returns:
trueif the given array is non-null and contains at least one NaN value.
-
hasNaN
public static boolean hasNaN(float[] array) Returnstrueif the specified array contains at least oneNaNvalue.- Parameters:
array- the array to check, ornull.- Returns:
trueif the given array is non-null and contains at least one NaN value.
-
allEquals
Returnstrueif 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:
trueif all elements in the given array are equal to the given value.- Since:
- 0.8
-
allEquals
public static boolean allEquals(double[] array, double value) Returnstrueif 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:
trueif all elements in the given array are equal to the given value.
-
allEquals
public static boolean allEquals(float[] array, float value) Returnstrueif 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:
trueif all elements in the given array are equal to the given value.
-
allEquals
public static boolean allEquals(int[] array, int value) Returnstrueif all values in the specified array are equal to the specified value.- Parameters:
array- the array to check.value- the expected value.- Returns:
trueif all elements in the given array are equal to the given value.- Since:
- 1.2
-
containsIgnoreCase
Returnstrueif 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:
trueif the array is non-null and contains the given value, orfalseotherwise.
-
containsIdentity
Returnstrueif 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
IdentityHashMapinstead.- Parameters:
array- the array to search in. May benulland may contains null elements.value- the value to search. May benull.- Returns:
trueif the array is non-null and contains the value (which may be null), orfalseotherwise.
-
contains
Returnstrueif 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
HashSetinstead.- Parameters:
array- the array to search in. May benulland may contains null elements.value- the value to search. May benull.- Returns:
trueif the array is non-null and contains the value (which may be null), orfalseotherwise.- See Also:
-
intersects
Returnstrueif 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
HashSetinstead.- Parameters:
array1- the first array, ornull.array2- the second array, ornull.- Returns:
trueif both array are non-null and have at least one element in common.- See Also:
-
concatenate
Returns the concatenation of all given arrays. This method performs the following checks:- If the
arraysargument isnullor contains onlynullelements, then this method returnsnull. - Otherwise if the
arraysargument 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
-
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
nullif the two given arrays were null. May be one of the given arrays. - See Also:
-