Class Arrays2


  • public final class Arrays2
    extends java.lang.Object
    Useful general array manipulations beyond what Arrays provides.
    Author:
    David M. Lloyd
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> E[] compactNulls​(E[] original)
      Create a new array from the original which contains no null values, possibly destroying the contents of the original array.
      static <E> E[] createArray​(java.lang.Class<E> elementType, int size)
      Create an array of the given size, ensuring type safety.
      static boolean equals​(byte[] a1, int offs1, byte[] a2)
      Compare two sub-regions of the given arrays.
      static boolean equals​(byte[] a1, int offs1, byte[] a2, int offs2, int len)
      Compare two sub-regions of the given arrays.
      static boolean equals​(char[] a1, int offs1, char[] a2)
      Compare two sub-regions of the given arrays.
      static boolean equals​(char[] a1, int offs1, char[] a2, int offs2, int len)
      Compare two sub-regions of the given arrays.
      static boolean equals​(char[] a1, int offs1, java.lang.String a2)
      Compare two sub-regions of the given array and string.
      static boolean equals​(char[] a1, int offs1, java.lang.String a2, int offs2, int len)
      Compare two sub-regions of the given array and string.
      static boolean equals​(java.lang.String a1, char[] a2)
      Compare the given array and string.
      static boolean equals​(java.lang.String a1, int offs1, char[] a2)
      Compare two sub-regions of the given array and string.
      static int indexOf​(byte[] array, int search)
      Find the first occurrence of a byte in a byte array.
      static int indexOf​(byte[] array, int search, int offs)
      Find the first occurrence of a byte in a byte array.
      static int indexOf​(byte[] array, int search, int offs, int len)
      Find the first occurrence of a byte in a byte array.
      static java.lang.String objectToString​(java.lang.Object value)
      Deeply convert an object to a string, expanding arrays as they are encountered.
      static <T> T[] of​(T... items)
      Return the varargs list as its array representation.
      static java.lang.String toString​(byte[] bytes)
      Render the given byte array as a hexadecimal string in big-endian order.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • equals

        public static boolean equals​(byte[] a1,
                                     int offs1,
                                     byte[] a2,
                                     int offs2,
                                     int len)
        Compare two sub-regions of the given arrays.
        Parameters:
        a1 - the first array (must not be null)
        offs1 - the offset into the first array
        a2 - the second array (must not be null)
        offs2 - the offset into the second array
        len - the length to compare
        Returns:
        true if the regions are equal, or false if the regions are not equal or a length or offset is out of bounds for an array
      • equals

        public static boolean equals​(byte[] a1,
                                     int offs1,
                                     byte[] a2)
        Compare two sub-regions of the given arrays.
        Parameters:
        a1 - the first array (must not be null)
        offs1 - the offset into the first array
        a2 - the second array (must not be null)
        Returns:
        true if the regions are equal, or false if the regions are not equal or a length or offset is out of bounds for an array
      • equals

        public static boolean equals​(char[] a1,
                                     int offs1,
                                     char[] a2,
                                     int offs2,
                                     int len)
        Compare two sub-regions of the given arrays.
        Parameters:
        a1 - the first array (must not be null)
        offs1 - the offset into the first array
        a2 - the second array (must not be null)
        offs2 - the offset into the second array
        len - the length to compare
        Returns:
        true if the regions are equal, or false if the regions are not equal or a length or offset is out of bounds for an array
      • equals

        public static boolean equals​(char[] a1,
                                     int offs1,
                                     char[] a2)
        Compare two sub-regions of the given arrays.
        Parameters:
        a1 - the first array (must not be null)
        offs1 - the offset into the first array
        a2 - the second array (must not be null)
        Returns:
        true if the regions are equal, or false if the regions are not equal or a length or offset is out of bounds for an array
      • equals

        public static boolean equals​(char[] a1,
                                     int offs1,
                                     java.lang.String a2,
                                     int offs2,
                                     int len)
        Compare two sub-regions of the given array and string.
        Parameters:
        a1 - the array (must not be null)
        offs1 - the offset into the array
        a2 - the string (must not be null)
        offs2 - the offset into the string
        len - the length to compare
        Returns:
        true if the regions are equal, or false if the regions are not equal or a length or offset is out of bounds for the array or string
      • equals

        public static boolean equals​(char[] a1,
                                     int offs1,
                                     java.lang.String a2)
        Compare two sub-regions of the given array and string.
        Parameters:
        a1 - the array (must not be null)
        offs1 - the offset into the array
        a2 - the string (must not be null)
        Returns:
        true if the regions are equal, or false if the regions are not equal or a length or offset is out of bounds for the array or string
      • equals

        public static boolean equals​(java.lang.String a1,
                                     int offs1,
                                     char[] a2)
        Compare two sub-regions of the given array and string.
        Parameters:
        a1 - the string (must not be null)
        offs1 - the offset into the string
        a2 - the array (must not be null)
        Returns:
        true if the regions are equal, or false if the regions are not equal or a length or offset is out of bounds for the array or string
      • equals

        public static boolean equals​(java.lang.String a1,
                                     char[] a2)
        Compare the given array and string.
        Parameters:
        a1 - the string (must not be null)
        a2 - the array (must not be null)
        Returns:
        true if the regions are equal, or false if the regions are not equal or a length or offset is out of bounds for the array or string
      • of

        @SafeVarargs
        public static <T> T[] of​(T... items)
        Return the varargs list as its array representation.
        Type Parameters:
        T - the item type
        Parameters:
        items - the items
        Returns:
        the array
      • toString

        public static java.lang.String toString​(byte[] bytes)
        Render the given byte array as a hexadecimal string in big-endian order.
        Parameters:
        bytes - the byte array (must not be null)
        Returns:
        the string
      • indexOf

        public static int indexOf​(byte[] array,
                                  int search,
                                  int offs,
                                  int len)
        Find the first occurrence of a byte in a byte array.
        Parameters:
        array - the array to search
        search - the byte to search for
        offs - the offset in the array to start searching
        len - the length of the segment to search
        Returns:
        the index, or -1 if the byte is not found
      • indexOf

        public static int indexOf​(byte[] array,
                                  int search,
                                  int offs)
        Find the first occurrence of a byte in a byte array.
        Parameters:
        array - the array to search
        search - the byte to search for
        offs - the offset in the array to start searching
        Returns:
        the index, or -1 if the byte is not found
      • indexOf

        public static int indexOf​(byte[] array,
                                  int search)
        Find the first occurrence of a byte in a byte array.
        Parameters:
        array - the array to search
        search - the byte to search for
        Returns:
        the index, or -1 if the byte is not found
      • createArray

        public static <E> E[] createArray​(java.lang.Class<E> elementType,
                                          int size)
        Create an array of the given size, ensuring type safety.
        Type Parameters:
        E - the element type
        Parameters:
        elementType - the element type class
        size - the array size
        Returns:
        the array
      • compactNulls

        public static <E> E[] compactNulls​(E[] original)
        Create a new array from the original which contains no null values, possibly destroying the contents of the original array. If the original contains no null values, the original array is returned.
        Type Parameters:
        E - the element type
        Parameters:
        original - the original array (not null, will be modified)
        Returns:
        the compacted (possibly empty) array
      • objectToString

        public static java.lang.String objectToString​(java.lang.Object value)
        Deeply convert an object to a string, expanding arrays as they are encountered.
        Parameters:
        value - the value to convert
        Returns:
        the string