Class ArrayExtensions


  • public final class ArrayExtensions
    extends java.lang.Object
    The class ArrayExtensions is an extensions class for use with array objects.
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayExtensions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] arraycopyWithSystem​(T[] source, T[] destination)
      Copy the given source array to the given destination array.

      Note: the given destination array have to be already initialized with the size of the source.

      Example:

      Integer[] source = {1,2,3,4,5,6,7,8,9};
      Integer[] destination = new Integer[source.length];
      destination = ArrayExtensions.arraycopyWithSystem(source, destination);
      static <T> java.util.List<T> asList​(T[] array)
      Creates a new List from the given array.
      static <T> java.util.Set<T> asSet​(T... array)
      Creates a new Set from the given array.
      static <T> boolean contains​(T[] array, T element)
      Returns true if and only if the given element is in the given array
      static <T> T getFirst​(T[] array)
      Gets the first object from the given array.
      static <T> int getIndex​(T[] array, T element)
      Gets the index of the given element in the given array.
      static <T> T getLast​(T[] array)
      Gets the last object from the given array.
      static <T> int getNextIndex​(T[] array, T element)
      Gets the next index of the given array.
      static <T> int[] getNextIndexes​(T[] array, T element, int count)
      Gets the next indexes from the given count of the given array.
      static <T> int getPreviousIndex​(T[] array, T element)
      Gets the previous index of the given array.
      static <T> int[] getPreviousIndexes​(T[] array, T element, int count)
      Gets the previous indexes from the given count of the given array.
      static <T> int indexOf​(T[] array, T element)
      Gets the index of the given element in the given array.
      static <T> T[] intersection​(@NonNull T[] one, @NonNull T[] other)
      Intersection of the given two arrays.
      static <T> boolean isFirst​(T[] array, T element)
      Checks if the given element is the first in the given array.
      static <T> boolean isLast​(T[] array, T element)
      Checks if the given element is the last in the given array.
      static <T> T[] remove​(@NonNull T[] array, int... indexes)
      Removes the first element of the array.
      static <T> T[] removeAll​(@NonNull T[] array, @NonNull T[] arrayToRemove)
      Removes the first element of the array.
      static <T> T[] removeFirst​(@NonNull T[] original)
      Creates a new array cloned from the given array with the difference that the first element is removed
      static <T> T[] removeFromEnd​(T[] array, T[] suffix)
      Removes the given suffix array from the first given array
      static <T> T[] removeFromStart​(@NonNull T[] array, @NonNull T[] prefix)
      Removes the given prefix array from the first given array
      static <T> T[] removeLast​(@NonNull T[] original)
      Removes the first element of the array
      static byte[][] splitInChunks​(byte[] bytes, int chunkSize)
      Split the given byte array in to new arrays with the chunk size.
      static <T> java.util.List<T> toList​(T[] array)
      Creates a new List from the given array.
      • Methods inherited from class java.lang.Object

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

      • ArrayExtensions

        public ArrayExtensions()
    • Method Detail

      • removeFromStart

        public static <T> T[] removeFromStart​(@NonNull
                                              @NonNull T[] array,
                                              @NonNull
                                              @NonNull T[] prefix)
        Removes the given prefix array from the first given array
        Type Parameters:
        T - the generic type of the objects in the arrays
        Parameters:
        array - the array
        prefix - the prefix
        Returns:
        the resulted array
      • removeFromEnd

        public static <T> T[] removeFromEnd​(T[] array,
                                            T[] suffix)
        Removes the given suffix array from the first given array
        Type Parameters:
        T - the generic type of the objects in the arrays
        Parameters:
        array - the array
        suffix - the suffix
        Returns:
        the resulted array
      • removeAll

        public static <T> T[] removeAll​(@NonNull
                                        @NonNull T[] array,
                                        @NonNull
                                        @NonNull T[] arrayToRemove)
        Removes the first element of the array.
        Type Parameters:
        T - the generic type of the objects in the array
        Parameters:
        array - the origin array
        arrayToRemove - the array to remove
        Returns:
        the new created array with the elements
      • contains

        public static <T> boolean contains​(T[] array,
                                           T element)
        Returns true if and only if the given element is in the given array
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        Returns:
        true if and only if the given element is in the given array otherwise false
      • remove

        public static <T> T[] remove​(@NonNull
                                     @NonNull T[] array,
                                     int... indexes)
        Removes the first element of the array.
        Type Parameters:
        T - the generic type of the objects in the array
        Parameters:
        array - the origin array
        indexes - the indexes to remove
        Returns:
        the new created array with the elements from the given indexes
      • removeFirst

        public static <T> T[] removeFirst​(@NonNull
                                          @NonNull T[] original)
        Creates a new array cloned from the given array with the difference that the first element is removed
        Type Parameters:
        T - the generic type of the objects in the array
        Parameters:
        original - the origin array
        Returns:
        the new created array with out the first element
      • removeLast

        public static <T> T[] removeLast​(@NonNull
                                         @NonNull T[] original)
        Removes the first element of the array
        Type Parameters:
        T - the generic type of the objects in the array
        Parameters:
        original - the origin array
        Returns:
        the new created array with out the first element
      • arraycopyWithSystem

        public static <T> T[] arraycopyWithSystem​(T[] source,
                                                  T[] destination)
        Copy the given source array to the given destination array.

        Note: the given destination array have to be already initialized with the size of the source.

        Example:

        Integer[] source = {1,2,3,4,5,6,7,8,9};
        Integer[] destination = new Integer[source.length];
        destination = ArrayExtensions.arraycopyWithSystem(source, destination);
        Type Parameters:
        T - the generic type of the objects in the array
        Parameters:
        source - the source
        destination - the destination
        Returns:
        the t[]
      • asSet

        @SafeVarargs
        public static <T> java.util.Set<T> asSet​(T... array)
        Creates a new Set from the given array.

        Type Parameters:
        T - the generic type of the objects in the array.
        Parameters:
        array - the array
        Returns:
        the new Set created from the given array.
      • asList

        public static <T> java.util.List<T> asList​(T[] array)
        Creates a new List from the given array.

        Note: This wraps only the method asList from Arrays.asList(Object...).
        Type Parameters:
        T - the generic type of the objects in the array.
        Parameters:
        array - the array
        Returns:
        the new List created from the given array.
      • getFirst

        public static <T> T getFirst​(T[] array)
        Gets the first object from the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array.
        Returns:
        Returns the first object from the given array or null if the array is empty or null.
      • getIndex

        public static <T> int getIndex​(T[] array,
                                       T element)
        Gets the index of the given element in the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        Returns:
        the int
      • getLast

        public static <T> T getLast​(T[] array)
        Gets the last object from the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array.
        Returns:
        Returns the last object from the given array or null if the array is empty or null.
      • getNextIndex

        public static <T> int getNextIndex​(T[] array,
                                           T element)
        Gets the next index of the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        Returns:
        the next index
      • getNextIndexes

        public static <T> int[] getNextIndexes​(T[] array,
                                               T element,
                                               int count)
        Gets the next indexes from the given count of the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        count - the count
        Returns:
        the next indexes or null if there is no next indexes.
      • getPreviousIndex

        public static <T> int getPreviousIndex​(T[] array,
                                               T element)
        Gets the previous index of the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        Returns:
        the previous index
      • getPreviousIndexes

        public static <T> int[] getPreviousIndexes​(T[] array,
                                                   T element,
                                                   int count)
        Gets the previous indexes from the given count of the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        count - the count
        Returns:
        the previous indexes or null if there is no previous indexes.
      • intersection

        public static <T> T[] intersection​(@NonNull
                                           @NonNull T[] one,
                                           @NonNull
                                           @NonNull T[] other)
        Intersection of the given two arrays.
        Type Parameters:
        T - the generic type
        Parameters:
        one - the first array
        other - the other array
        Returns:
        the result of the intersection
      • indexOf

        public static <T> int indexOf​(T[] array,
                                      T element)
        Gets the index of the given element in the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        Returns:
        the int
      • isFirst

        public static <T> boolean isFirst​(T[] array,
                                          T element)
        Checks if the given element is the first in the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        Returns:
        true if the given element is the first otherwise false
      • isLast

        public static <T> boolean isLast​(T[] array,
                                         T element)
        Checks if the given element is the last in the given array.
        Type Parameters:
        T - the generic type
        Parameters:
        array - the array
        element - the element
        Returns:
        true if the given element is the last otherwise false
      • splitInChunks

        public static byte[][] splitInChunks​(byte[] bytes,
                                             int chunkSize)
        Split the given byte array in to new arrays with the chunk size.
        Parameters:
        bytes - the bytes
        chunkSize - the chunk size
        Returns:
        the byte[][]
      • toList

        public static <T> java.util.List<T> toList​(T[] array)
        Creates a new List from the given array.

        Note: This wraps only the method asList from Arrays.asList(Object...).
        Type Parameters:
        T - the generic type of the objects in the array.
        Parameters:
        array - the array
        Returns:
        the new List created from the given array.