Package org.scijava.util
Class ArrayUtils
- java.lang.Object
-
- org.scijava.util.ArrayUtils
-
public final class ArrayUtils extends Object
Utility class for working with arrays, particularly arrays of primitives.- Author:
- Mark Hiner, Curtis Rueden, Melissa Linkert, Chris Allan
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]allocate(long... sizes)Allocates a 1-dimensional byte array matching the product of the given sizes.static <T> T[]array(T... values)Creates an array of the given type, containing the specified values.static booleancontains(boolean[] array, boolean value)Returns true if the given value is contained in the specified array.static booleancontains(byte[] array, byte value)Returns true if the given value is contained in the specified array.static booleancontains(char[] array, char value)Returns true if the given value is contained in the specified array.static booleancontains(double[] array, double value)Returns true if the given value is contained in the specified array.static booleancontains(float[] array, float value)Returns true if the given value is contained in the specified array.static booleancontains(int[] array, int value)Returns true if the given value is contained in the specified array.static booleancontains(long[] array, long value)Returns true if the given value is contained in the specified array.static booleancontains(short[] array, short value)Returns true if the given value is contained in the specified array.static booleancontains(Object[] array, Object value)Returns true if the given value is contained in the specified array.static intindexOf(boolean[] array, boolean value)Returns the index of the first occurrence of the given value in the given array.static intindexOf(byte[] array, byte value)Returns the index of the first occurrence of the given value in the given array.static intindexOf(char[] array, char value)Returns the index of the first occurrence of the given value in the given array.static intindexOf(double[] array, double value)Returns the index of the first occurrence of the given value in the given array.static intindexOf(float[] array, float value)Returns the index of the first occurrence of the given value in the given array.static intindexOf(int[] array, int value)Returns the index of the first occurrence of the given value in the given array.static intindexOf(long[] array, long value)Returns the index of the first occurrence of the given value in the given array.static intindexOf(short[] array, short value)Returns the index of the first occurrence of the given value in the given array.static intindexOf(Object[] array, Object value)Returns the index of the first occurrence of the given value in the given Object array.static intsafeMultiply32(long... sizes)Checks that the product of the given sizes does not exceed the 32-bit integer limit (i.e.,Integer.MAX_VALUE).static longsafeMultiply64(long... sizes)Checks that the product of the given sizes does not exceed the 64-bit integer limit (i.e.,Long.MAX_VALUE).static Collection<?>toCollection(Object value)Converts the provided Object to aCollectionimplementation.
-
-
-
Method Detail
-
array
@SafeVarargs public static <T> T[] array(T... values)
Creates an array of the given type, containing the specified values.
-
toCollection
public static Collection<?> toCollection(Object value)
Converts the provided Object to aCollectionimplementation. If the object is an array type, aPrimitiveArraywrapper will be created.
-
allocate
public static byte[] allocate(long... sizes) throws IllegalArgumentExceptionAllocates a 1-dimensional byte array matching the product of the given sizes.- Parameters:
sizes- list of sizes from which to allocate the array- Returns:
- a byte array of the appropriate size
- Throws:
IllegalArgumentException- if the total size exceeds 2GB, which is the maximum size of an array in Java; or if any size argument is zero or negative
-
safeMultiply32
public static int safeMultiply32(long... sizes) throws IllegalArgumentExceptionChecks that the product of the given sizes does not exceed the 32-bit integer limit (i.e.,Integer.MAX_VALUE).- Parameters:
sizes- list of sizes from which to compute the product- Returns:
- the product of the given sizes
- Throws:
IllegalArgumentException- if the total size exceeds 2GiB, which is the maximum size of an int in Java; or if any size argument is zero or negative
-
safeMultiply64
public static long safeMultiply64(long... sizes) throws IllegalArgumentExceptionChecks that the product of the given sizes does not exceed the 64-bit integer limit (i.e.,Long.MAX_VALUE).- Parameters:
sizes- list of sizes from which to compute the product- Returns:
- the product of the given sizes
- Throws:
IllegalArgumentException- if the total size exceeds 8EiB, which is the maximum size of a long in Java; or if any size argument is zero or negative
-
contains
public static boolean contains(byte[] array, byte value)Returns true if the given value is contained in the specified array.
-
contains
public static boolean contains(boolean[] array, boolean value)Returns true if the given value is contained in the specified array.
-
contains
public static boolean contains(char[] array, char value)Returns true if the given value is contained in the specified array.
-
contains
public static boolean contains(double[] array, double value)Returns true if the given value is contained in the specified array.
-
contains
public static boolean contains(float[] array, float value)Returns true if the given value is contained in the specified array.
-
contains
public static boolean contains(int[] array, int value)Returns true if the given value is contained in the specified array.
-
contains
public static boolean contains(long[] array, long value)Returns true if the given value is contained in the specified array.
-
contains
public static boolean contains(short[] array, short value)Returns true if the given value is contained in the specified array.
-
contains
public static boolean contains(Object[] array, Object value)
Returns true if the given value is contained in the specified array.
-
indexOf
public static int indexOf(boolean[] array, boolean value)Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
-
indexOf
public static int indexOf(byte[] array, byte value)Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
-
indexOf
public static int indexOf(char[] array, char value)Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
-
indexOf
public static int indexOf(double[] array, double value)Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
-
indexOf
public static int indexOf(float[] array, float value)Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
-
indexOf
public static int indexOf(int[] array, int value)Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
-
indexOf
public static int indexOf(long[] array, long value)Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
-
indexOf
public static int indexOf(short[] array, short value)Returns the index of the first occurrence of the given value in the given array. If the value is not in the array, returns -1.
-
-