Class 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 boolean contains​(boolean[] array, boolean value)
      Returns true if the given value is contained in the specified array.
      static boolean contains​(byte[] array, byte value)
      Returns true if the given value is contained in the specified array.
      static boolean contains​(char[] array, char value)
      Returns true if the given value is contained in the specified array.
      static boolean contains​(double[] array, double value)
      Returns true if the given value is contained in the specified array.
      static boolean contains​(float[] array, float value)
      Returns true if the given value is contained in the specified array.
      static boolean contains​(int[] array, int value)
      Returns true if the given value is contained in the specified array.
      static boolean contains​(long[] array, long value)
      Returns true if the given value is contained in the specified array.
      static boolean contains​(short[] array, short value)
      Returns true if the given value is contained in the specified array.
      static boolean contains​(Object[] array, Object value)
      Returns true if the given value is contained in the specified array.
      static int indexOf​(boolean[] array, boolean value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(byte[] array, byte value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(char[] array, char value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(double[] array, double value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(float[] array, float value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(int[] array, int value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(long[] array, long value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(short[] array, short value)
      Returns the index of the first occurrence of the given value in the given array.
      static int indexOf​(Object[] array, Object value)
      Returns the index of the first occurrence of the given value in the given Object array.
      static int safeMultiply32​(long... sizes)
      Checks that the product of the given sizes does not exceed the 32-bit integer limit (i.e., Integer.MAX_VALUE).
      static long safeMultiply64​(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 a Collection implementation.
    • 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 a Collection implementation. If the object is an array type, a PrimitiveArray wrapper will be created.
      • allocate

        public static byte[] allocate​(long... sizes)
                               throws IllegalArgumentException
        Allocates 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 IllegalArgumentException
        Checks 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 IllegalArgumentException
        Checks 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.
      • indexOf

        public static int indexOf​(Object[] array,
                                  Object value)
        Returns the index of the first occurrence of the given value in the given Object array. If the value is not in the array, returns -1.