Class ArrayUtil


  • public final class ArrayUtil
    extends Object
    Some utilities functions for arrays.
    Version:
    17.0 2020-01-04 14:41:38
    Author:
    Stéphane GALLAND
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    util
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  ArrayUtil.Filter<T>
      Some utilities functions for arrays.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <I,​T>
      T[]
      castArray​(I[] originalArray, Class<T> clazz)
      Cast the specified array and put null is the array when the element could not be casted.
      static <I,​O>
      O[]
      castArray​(Collection<I> originalArray, Class<O> clazz)
      Cast the specified array and put null is the array when the element could not be casted.
      static <I,​O>
      O[]
      castRestrictedArray​(I[] originalArray, Class<O> clazz)
      Cast the specified array and remove the elements that could not be casted.
      static <I,​O>
      O[]
      castRestrictedArray​(Collection<I> originalArray, Class<O> clazz)
      Cast the specified array and remove the elements that could not be casted.
      static <T> boolean contains​(Comparator<T> comparator, T elt, T... array)
      Replies of the given element is in the sorted array.
      static <T> boolean contains​(T elt, T... array)
      Replies of the given element is in the array.
      static <T> boolean containsAll​(T[] elts, T[] array)
      Replies if the given elements is in the array.
      static <T> boolean containsAllObjects​(T[] elts, T[] array)
      Replies of the given element is in the array.
      static <T> boolean containsObject​(T elt, T[] array)
      Replies of the given element is in the array.
      static <T> boolean intersects​(T[] elts, T[] array)
      Replies of one of the given elements is in the array.
      static Iterator<Boolean> iterator​(boolean[] array)
      Replies a sized iterator on the objects.
      static Iterator<Byte> iterator​(byte[] array)
      Replies a sized iterator on the objects.
      static Iterator<Character> iterator​(char[] array)
      Replies a sized iterator on the objects.
      static Iterator<Double> iterator​(double[] array)
      Replies a sized iterator on the objects.
      static Iterator<Float> iterator​(float[] array)
      Replies a sized iterator on the objects.
      static Iterator<Integer> iterator​(int[] array)
      Replies a sized iterator on the objects.
      static Iterator<Long> iterator​(long[] array)
      Replies a sized iterator on the objects.
      static Iterator<Short> iterator​(short[] array)
      Replies a sized iterator on the objects.
      static <T> Iterator<T> iterator​(T[] array)
      Replies a sized iterator on the objects.
      static <T> T[] merge​(Class<T> clazz, T... elements)
      Merge the elements to make an array.
      static <T> T[] merge​(Class<T> clazz, T[]... arrays)
      Merge the arrays.
      static <T> T[] merge​(Class<T> clazz, T[] source, T... elements)
      Merge the elements to make an array.
      static <T> T[] mergeWithoutNull​(Class<T> clazz, T... elements)
      Merge the elements to make an array.
      static <T> T[] mergeWithoutNull​(Class<T> clazz, T[]... arrays)
      Merge the arrays.
      static <T> T[] mergeWithoutNull​(Class<T> clazz, T[] source, T... elements)
      Merge the elements to make an array.
      static <T> T[] newInstance​(Class<T> clazz, int size)
      Create an instance of array.
      static <T> T[] removeElements​(Class<T> clazz, T[] source, T... toRemove)
      Remove the given elements from the array.
      static <T> T[] restrictArray​(T[] originalArray, Class<T> clazz, ArrayUtil.Filter<T> comparator)
      Replies an array in which all the elements must respect the given comparator.
      static <T> void reverse​(T[] tab)
      Reverse the specified array.
      static void shuffle​(boolean[] array)
      Shuffle the specified array.
      static void shuffle​(boolean[] array, Random rnd)
      Shuffle the specified array.
      static void shuffle​(byte[] array)
      Shuffle the specified array.
      static void shuffle​(byte[] array, Random rnd)
      Shuffle the specified array.
      static void shuffle​(char[] array)
      Shuffle the specified array.
      static void shuffle​(char[] array, Random rnd)
      Shuffle the specified array.
      static void shuffle​(double[] array)
      Shuffle the specified array.
      static void shuffle​(double[] array, Random rnd)
      Shuffle the specified array.
      static void shuffle​(float[] array)
      Shuffle the specified array.
      static void shuffle​(float[] array, Random rnd)
      Shuffle the specified array.
      static void shuffle​(int[] array)
      Shuffle the specified array.
      static void shuffle​(int[] array, Random rnd)
      Shuffle the specified array.
      static void shuffle​(long[] array)
      Shuffle the specified array.
      static void shuffle​(long[] array, Random rnd)
      Shuffle the specified array.
      static <T> void shuffle​(T[] array)
      Shuffle the specified array.
      static <T> void shuffle​(T[] array, Random rnd)
      Shuffle the specified array.
      static SizedIterator<Boolean> sizedIterator​(boolean[] array)
      Replies a sized iterator on the objects.
      static SizedIterator<Byte> sizedIterator​(byte[] array)
      Replies a sized iterator on the objects.
      static SizedIterator<Character> sizedIterator​(char[] array)
      Replies a sized iterator on the objects.
      static SizedIterator<Double> sizedIterator​(double[] array)
      Replies a sized iterator on the objects.
      static SizedIterator<Float> sizedIterator​(float[] array)
      Replies a sized iterator on the objects.
      static SizedIterator<Integer> sizedIterator​(int[] array)
      Replies a sized iterator on the objects.
      static SizedIterator<Long> sizedIterator​(long[] array)
      Replies a sized iterator on the objects.
      static SizedIterator<Short> sizedIterator​(short[] array)
      Replies a sized iterator on the objects.
      static <T> SizedIterator<T> sizedIterator​(T[] array)
      Replies a sized iterator on the objects.
      static <T> T[] toArray​(Collection<? extends T> collection, Class<T> clazz)
      Replies an array that corresponds to the given collection.
      static <T> T[] toArrayAndClear​(Collection<? extends T> collection, Class<T> clazz)
      Replies an array that corresponds to the given collection.
      static String toString​(Object obj)
      Replies a string representation of the given object.
    • Method Detail

      • reverse

        public static <T> void reverse​(T[] tab)
        Reverse the specified array.
        Type Parameters:
        T - the type of the data in the array.
        Parameters:
        tab - the array.
      • toArray

        @Pure
        public static <T> T[] toArray​(Collection<? extends T> collection,
                                      Class<T> clazz)
        Replies an array that corresponds to the given collection.
        Type Parameters:
        T - is the type of the elements.
        Parameters:
        collection - is the collection to translate
        clazz - is the type of the elements.
        Returns:
        the array.
      • toArrayAndClear

        public static <T> T[] toArrayAndClear​(Collection<? extends T> collection,
                                              Class<T> clazz)
        Replies an array that corresponds to the given collection.

        This function clear the content of the given collection.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        collection - is the collection to translate
        clazz - is the type of the elements.
        Returns:
        the array.
      • merge

        @Pure
        public static <T> T[] merge​(Class<T> clazz,
                                    T[]... arrays)
        Merge the arrays.

        This function does not remove the null values.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        clazz - is the type of the elements.
        arrays - are the arrays to merge.
        Returns:
        the array.
      • merge

        @Pure
        public static <T> T[] merge​(Class<T> clazz,
                                    T... elements)
        Merge the elements to make an array.

        This function does not remove the null values.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        clazz - is the type of the elements.
        elements - are the elements to merge.
        Returns:
        the array.
      • merge

        @Pure
        public static <T> T[] merge​(Class<T> clazz,
                                    T[] source,
                                    T... elements)
        Merge the elements to make an array.

        This function does not remove the null values.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        source - is the first array to merge.
        clazz - is the type of the elements.
        elements - are the elements to merge.
        Returns:
        the array.
      • mergeWithoutNull

        @Pure
        public static <T> T[] mergeWithoutNull​(Class<T> clazz,
                                               T[]... arrays)
        Merge the arrays.

        This function removes the null values.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        clazz - is the type of the elements.
        arrays - are the arrays to merge.
        Returns:
        the array.
      • mergeWithoutNull

        @Pure
        public static <T> T[] mergeWithoutNull​(Class<T> clazz,
                                               T... elements)
        Merge the elements to make an array.

        This function removes the null values.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        clazz - is the type of the elements.
        elements - are the elements to merge.
        Returns:
        the array.
      • mergeWithoutNull

        @Pure
        public static <T> T[] mergeWithoutNull​(Class<T> clazz,
                                               T[] source,
                                               T... elements)
        Merge the elements to make an array.

        This function removes the null values.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        source - is the first array to merge.
        clazz - is the type of the elements.
        elements - are the elements to merge.
        Returns:
        the array.
      • removeElements

        @Pure
        public static <T> T[] removeElements​(Class<T> clazz,
                                             T[] source,
                                             T... toRemove)
        Remove the given elements from the array.
        Type Parameters:
        T - is the type of the elements.
        Parameters:
        clazz - is the type of the elements.
        source - is the array to scan
        toRemove - are the elements to remove.
        Returns:
        the array without the removed elements.
      • castArray

        @Pure
        public static <I,​T> T[] castArray​(I[] originalArray,
                                                Class<T> clazz)
        Cast the specified array and put null is the array when the element could not be casted.
        Type Parameters:
        I - is the type of the elements before the cast.
        T - is the type of the elements after the cast.
        Parameters:
        originalArray - is the array to cast
        clazz - is the casting type
        Returns:
        the array in which each element was casted according to the given type.
        See Also:
        castRestrictedArray(Object[], Class)
      • castArray

        @Pure
        public static <I,​O> O[] castArray​(Collection<I> originalArray,
                                                Class<O> clazz)
        Cast the specified array and put null is the array when the element could not be casted.
        Type Parameters:
        I - is the type of the elements before the cast.
        O - is the type of the elements after the cast.
        Parameters:
        originalArray - is the array to cast
        clazz - is the casting type
        Returns:
        the array in which each element was casted according to the given type.
        See Also:
        castRestrictedArray(Object[], Class)
      • restrictArray

        @Pure
        public static <T> T[] restrictArray​(T[] originalArray,
                                            Class<T> clazz,
                                            ArrayUtil.Filter<T> comparator)
        Replies an array in which all the elements must respect the given comparator.

        The respect of the comparator is done when the comparator replies equals.

        Type Parameters:
        T - is the type of the elements
        Parameters:
        originalArray - is the array to cast
        clazz - is the casting type
        comparator - is filtering the elements.
        Returns:
        the array in which each element was casted according to the given type.
      • castRestrictedArray

        @Pure
        public static <I,​O> O[] castRestrictedArray​(I[] originalArray,
                                                          Class<O> clazz)
        Cast the specified array and remove the elements that could not be casted.
        Type Parameters:
        I - is the type of the elements before the cast.
        O - is the type of the elements after the cast.
        Parameters:
        originalArray - is the array to cast
        clazz - is the casting type
        Returns:
        the array in which each element was casted according to the given type.
        See Also:
        castArray(Object[], Class)
      • castRestrictedArray

        @Pure
        public static <I,​O> O[] castRestrictedArray​(Collection<I> originalArray,
                                                          Class<O> clazz)
        Cast the specified array and remove the elements that could not be casted.
        Type Parameters:
        I - is the type of the elements before the cast.
        O - is the type of the elements after the cast.
        Parameters:
        originalArray - is the array to cast
        clazz - is the casting type
        Returns:
        the array in which each element was casted according to the given type.
        See Also:
        castArray(Object[], Class)
      • containsObject

        @Pure
        public static <T> boolean containsObject​(T elt,
                                                 T[] array)
        Replies of the given element is in the array.

        This function does not call Object.equals(java.lang.Object). It tests the equality on the object references.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        elt - is the element to search for.
        array - is the array inside which the search must be done.
        Returns:
        true if the element is inside the array, otherwise false
      • containsAllObjects

        @Pure
        public static <T> boolean containsAllObjects​(T[] elts,
                                                     T[] array)
        Replies of the given element is in the array.

        This function does not call Object.equals(java.lang.Object). It tests the equality on the object references.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        elts - are the elements to search for.
        array - is the array inside which the search must be done.
        Returns:
        true if the elements are inside the array, otherwise false
      • contains

        @Pure
        public static <T> boolean contains​(T elt,
                                           T... array)
        Replies of the given element is in the array.

        This function is based on Object.equals(java.lang.Object).

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        elt - is the element to search for.
        array - is the array inside which the search must be done.
        Returns:
        true if the element is inside the array, otherwise false
      • contains

        @Pure
        public static <T> boolean contains​(Comparator<T> comparator,
                                           T elt,
                                           T... array)
        Replies of the given element is in the sorted array.

        This function assumes that the given array is sorted according to the given comparator. A dichotomic algorithm is used.

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        comparator - is the comparator used to sort the array.
        elt - is the element to search for.
        array - is the array inside which the search must be done.
        Returns:
        true if the element is inside the array, otherwise false
        Since:
        4.0
      • containsAll

        @Pure
        public static <T> boolean containsAll​(T[] elts,
                                              T[] array)
        Replies if the given elements is in the array.

        This function is based on Object.equals(java.lang.Object).

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        elts - are the elements to search for.
        array - is the array inside which the search must be done.
        Returns:
        true if the elements are inside the array, otherwise false
      • intersects

        @Pure
        public static <T> boolean intersects​(T[] elts,
                                             T[] array)
        Replies of one of the given elements is in the array.

        This function is based on Object.equals(java.lang.Object).

        Type Parameters:
        T - is the type of the elements.
        Parameters:
        elts - is the first array.
        array - is the second array.
        Returns:
        true if an intersection is existing, otherwise false
      • newInstance

        @Pure
        public static <T> T[] newInstance​(Class<T> clazz,
                                          int size)
        Create an instance of array.
        Type Parameters:
        T - is the type of the elements.
        Parameters:
        clazz - is the type of the elements.
        size - is the size of the new array.
        Returns:
        the new array.
      • shuffle

        @Pure
        public static <T> void shuffle​(T[] array)
        Shuffle the specified array.
        Type Parameters:
        T - is the type of the elements.
        Parameters:
        array - is the array to shuffle.
      • shuffle

        @Pure
        public static <T> void shuffle​(T[] array,
                                       Random rnd)
        Shuffle the specified array.
        Type Parameters:
        T - is the type of the elements.
        Parameters:
        array - is the array to shuffle.
        rnd - is the random number generator to use.
      • shuffle

        @Pure
        public static void shuffle​(boolean[] array)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
      • shuffle

        @Pure
        public static void shuffle​(boolean[] array,
                                   Random rnd)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
        rnd - is the random number generator to use.
      • shuffle

        @Pure
        public static void shuffle​(byte[] array)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
      • shuffle

        @Pure
        public static void shuffle​(byte[] array,
                                   Random rnd)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
        rnd - is the random number generator to use.
      • shuffle

        @Pure
        public static void shuffle​(char[] array)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
      • shuffle

        @Pure
        public static void shuffle​(char[] array,
                                   Random rnd)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
        rnd - is the random number generator to use.
      • shuffle

        @Pure
        public static void shuffle​(int[] array)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
      • shuffle

        @Pure
        public static void shuffle​(int[] array,
                                   Random rnd)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
        rnd - is the random number generator to use.
      • shuffle

        @Pure
        public static void shuffle​(long[] array)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
      • shuffle

        @Pure
        public static void shuffle​(long[] array,
                                   Random rnd)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
        rnd - is the random number generator to use.
      • shuffle

        @Pure
        public static void shuffle​(float[] array)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
      • shuffle

        @Pure
        public static void shuffle​(float[] array,
                                   Random rnd)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
        rnd - is the random number generator to use.
      • shuffle

        @Pure
        public static void shuffle​(double[] array)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
      • shuffle

        @Pure
        public static void shuffle​(double[] array,
                                   Random rnd)
        Shuffle the specified array.
        Parameters:
        array - is the array to shuffle.
        rnd - is the random number generator to use.
      • toString

        @Pure
        public static String toString​(Object obj)
        Replies a string representation of the given object.

        This function supports the base type's arrays.

        Parameters:
        obj - is the object to translate.
        Returns:
        a string representation of the given object.
      • sizedIterator

        @Pure
        public static <T> SizedIterator<T> sizedIterator​(T[] array)
        Replies a sized iterator on the objects.
        Type Parameters:
        T - is the type of the object to iterate on.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
      • sizedIterator

        @Pure
        public static SizedIterator<Character> sizedIterator​(char[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • sizedIterator

        @Pure
        public static SizedIterator<Byte> sizedIterator​(byte[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • sizedIterator

        @Pure
        public static SizedIterator<Short> sizedIterator​(short[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • sizedIterator

        @Pure
        public static SizedIterator<Integer> sizedIterator​(int[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • sizedIterator

        @Pure
        public static SizedIterator<Long> sizedIterator​(long[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • sizedIterator

        @Pure
        public static SizedIterator<Float> sizedIterator​(float[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • sizedIterator

        @Pure
        public static SizedIterator<Double> sizedIterator​(double[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • sizedIterator

        @Pure
        public static SizedIterator<Boolean> sizedIterator​(boolean[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static <T> Iterator<T> iterator​(T[] array)
        Replies a sized iterator on the objects.
        Type Parameters:
        T - is the type of the object to iterate on.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static Iterator<Boolean> iterator​(boolean[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static Iterator<Character> iterator​(char[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static Iterator<Byte> iterator​(byte[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static Iterator<Short> iterator​(short[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static Iterator<Integer> iterator​(int[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static Iterator<Long> iterator​(long[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static Iterator<Float> iterator​(float[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1
      • iterator

        @Pure
        public static Iterator<Double> iterator​(double[] array)
        Replies a sized iterator on the objects.
        Parameters:
        array - are the objects to iterate on.
        Returns:
        an iterator
        Since:
        4.1