Class ArrayUtils

java.lang.Object
org.apache.juneau.internal.ArrayUtils

public final class ArrayUtils extends Object
Quick and dirty utilities for working with arrays.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T[]
    append(T[] array, T... newElements)
    Appends one or more elements to an array.
    static <T> Set<T>
    asSet(T[] array)
    Converts the specified array to a Set.
    static <E> E[]
    combine(E[]... arrays)
    Combine an arbitrary number of arrays into a single array.
    static boolean
    contains(String element, String[] array)
    Returns true if the specified array contains the specified element using the String.equals(Object) method.
    static <T> T[]
    copyOf(T[] array)
    Makes a copy of the specified array.
    static List
    copyToList(Object array, List list)
    Copies the specified array into the specified list.
    static boolean
    equals(String[] a1, String[] a2)
    Returns true if the following sorted arrays are equals.
    static int
    indexOf(String element, String[] array)
    Returns the index position of the element in the specified array using the String.equals(Object) method.
    static boolean
    isArray(Object array)
    Returns true if the specified object is an array.
    static final boolean
    Returns true if the specified array is null or has a length of zero.
    static final boolean
    isEmptyArray(Object[] array1, Object[] array2)
    Returns true if both specified arrays are null or have a length of zero.
    static final boolean
    Returns true if the specified array is not null and has a length greater than zero.
    static final <E> E[]
    reverse(E[] array)
    Reverses the entries in an array.
    static <E> Object
    toArray(Collection<?> c, Class<E> elementType)
    Converts the specified collection to an array.
    static <E> List<E>
    toList(Object array, Class<E> elementType)
    Converts the specified array to an ArrayList
    static List<Object>
    Recursively converts the specified array into a list of objects.
    static String[]
    Converts the specified collection to an array of strings.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • append

      public static <T> T[] append(T[] array, T... newElements)
      Appends one or more elements to an array.
      Type Parameters:
      T - The element type.
      Parameters:
      array - The array to append to.
      newElements - The new elements to append to the array.
      Returns:
      A new array with the specified elements appended.
    • combine

      public static <E> E[] combine(E[]... arrays)
      Combine an arbitrary number of arrays into a single array.
      Type Parameters:
      E - The element type.
      Parameters:
      arrays - Collection of arrays to combine.
      Returns:
      A new combined array, or null if all arrays are null.
    • asSet

      public static <T> Set<T> asSet(T[] array)
      Converts the specified array to a Set.

      The order of the entries in the set are the same as the array.

      Type Parameters:
      T - The entry type of the array.
      Parameters:
      array - The array being wrapped in a Set interface.
      Returns:
      The new set.
    • toArray

      public static <E> Object toArray(Collection<?> c, Class<E> elementType)
      Converts the specified collection to an array.

      Works on both object and primitive arrays.

      Type Parameters:
      E - The element type.
      Parameters:
      c - The collection to convert to an array.
      elementType - The component type of the collection.
      Returns:
      A new array.
    • isArray

      public static boolean isArray(Object array)
      Returns true if the specified object is an array.
      Parameters:
      array - The array to test.
      Returns:
      true if the specified object is an array.
    • toList

      public static <E> List<E> toList(Object array, Class<E> elementType)
      Converts the specified array to an ArrayList
      Type Parameters:
      E - The element type.
      Parameters:
      array - The array to convert.
      elementType - The type of objects in the array. It must match the actual component type in the array.
      Returns:
      A new ArrayList
    • toObjectList

      public static List<Object> toObjectList(Object array)
      Recursively converts the specified array into a list of objects.
      Parameters:
      array - The array to convert.
      Returns:
      A new ArrayList
    • copyToList

      public static List copyToList(Object array, List list)
      Copies the specified array into the specified list.

      Works on both object and primitive arrays.

      Parameters:
      array - The array to copy into a list.
      list - The list to copy the values into.
      Returns:
      The same list passed in.
    • contains

      public static boolean contains(String element, String[] array)
      Returns true if the specified array contains the specified element using the String.equals(Object) method.
      Parameters:
      element - The element to check for.
      array - The array to check.
      Returns:
      true if the specified array contains the specified element, false if the array or element is null.
    • indexOf

      public static int indexOf(String element, String[] array)
      Returns the index position of the element in the specified array using the String.equals(Object) method.
      Parameters:
      element - The element to check for.
      array - The array to check.
      Returns:
      The index position of the element in the specified array, or -1 if the array doesn't contain the element, or the array or element is null.
    • toStringArray

      public static String[] toStringArray(Collection<?> c)
      Converts the specified collection to an array of strings.

      Entries are converted to strings using Object.toString(). null values remain null.

      Parameters:
      c - The collection to convert.
      Returns:
      The collection as a string array.
    • equals

      public static boolean equals(String[] a1, String[] a2)
      Returns true if the following sorted arrays are equals.
      Parameters:
      a1 - Array #1.
      a2 - Array #2.
      Returns:
      true if the following sorted arrays are equals.
    • copyOf

      public static <T> T[] copyOf(T[] array)
      Makes a copy of the specified array.
      Type Parameters:
      T - The element type.
      Parameters:
      array - The array to copy.
      Returns:
      A new copy of the array, or null if the array was null.s
    • isNotEmptyArray

      public static final boolean isNotEmptyArray(Object[] array)
      Returns true if the specified array is not null and has a length greater than zero.
      Parameters:
      array - The array to check.
      Returns:
      true if the specified array is not null and has a length greater than zero.
    • isEmptyArray

      public static final boolean isEmptyArray(Object[] array)
      Returns true if the specified array is null or has a length of zero.
      Parameters:
      array - The array to check.
      Returns:
      true if the specified array is null or has a length of zero.
    • isEmptyArray

      public static final boolean isEmptyArray(Object[] array1, Object[] array2)
      Returns true if both specified arrays are null or have a length of zero.
      Parameters:
      array1 - The array to check.
      array2 - The array to check.
      Returns:
      true if the specified array is null or has a length of zero.
    • reverse

      public static final <E> E[] reverse(E[] array)
      Reverses the entries in an array.
      Type Parameters:
      E - The element type.
      Parameters:
      array - The array to reverse.
      Returns:
      The same array.