Class ArrayUtils

java.lang.Object
com.android.internal.util.ArrayUtils

public class ArrayUtils extends Object
Static utility methods for arrays that aren't already included in Arrays.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final File[]
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> android.util.ArraySet<T>
    add(android.util.ArraySet<T> cur, T val)
     
    static <T> ArrayList<T>
    add(ArrayList<T> cur, int index, T val)
     
    static <T> ArrayList<T>
    add(ArrayList<T> cur, T val)
     
    static <T> android.util.ArraySet<T>
    addAll(android.util.ArraySet<T> cur, Collection<T> val)
    Similar to Set.addAll(Collection)}, but with support for set values of null.
    static <T> T[]
    appendElement(Class<T> kind, T[] array, T element)
    Adds value to given array if not already present, providing set-like behavior.
    static <T> T[]
    appendElement(Class<T> kind, T[] array, T element, boolean allowDuplicates)
    Adds value to given array.
    static int[]
    appendInt(int[] cur, int val)
    Adds value to given array if not already present, providing set-like behavior.
    static int[]
    appendInt(int[] cur, int val, boolean allowDuplicates)
    Adds value to given array.
    static long[]
    appendLong(long[] cur, long val)
    Adds value to given array if not already present, providing set-like behavior.
    static long[]
    appendLong(long[] cur, long val, boolean allowDuplicates)
    Adds value to given array if not already present, providing set-like behavior.
    static void
    checkBounds(int len, int index)
    Throws ArrayIndexOutOfBoundsException if the index is out of bounds.
    static long[]
    cloneOrNull(long[] array)
     
    static <T> android.util.ArraySet<T>
    cloneOrNull(android.util.ArraySet<T> array)
     
    static <T> T[]
    cloneOrNull(T[] array)
    Clones an array or returns null if the array is null.
    static byte[]
    concat(byte[]... arrays)
    Returns the concatenation of the given byte arrays.
    static <T> T[]
    concat(Class<T> kind, T[]... arrays)
    Returns the concatenation of the given arrays.
    static boolean
    contains(char[] array, char value)
     
    static boolean
    contains(int[] array, int value)
     
    static boolean
    contains(long[] array, long value)
     
    static <T> boolean
    contains(Collection<T> cur, T val)
     
    static <T> boolean
    contains(T[] array, T value)
    Checks that value is present as at least one of the elements of the array.
    static <T> boolean
    containsAll(char[] array, char[] check)
    Test if all check items are contained in array.
    static <T> boolean
    containsAll(T[] array, T[] check)
    Test if all check items are contained in array.
    static <T> boolean
    containsAny(T[] array, T[] check)
    Test if any check items are contained in array.
    static int[]
    convertToIntArray(android.util.ArraySet<Integer> set)
     
    static int[]
    Deprecated.
    use IntArray instead
    static long[]
    convertToLongArray(int[] intArray)
     
    static String
     
    static int[]
    defeatNullable(int[] val)
     
    static File[]
     
    static String[]
     
    static <T> T[]
    emptyArray(Class<T> kind)
    Returns an empty array of the specified type.
    static <T> T[]
    emptyIfNull(T[] items, Class<T> kind)
    Returns the same array or an empty one if it's null.
    static boolean
    equals(byte[] array1, byte[] array2, int length)
    Checks if the beginnings of two byte arrays are equal.
    static <T> T[]
    filter(T[] items, IntFunction<T[]> arrayConstructor, Predicate<T> predicate)
    Returns an array containing elements from the given one that match the given predicate.
    static <T> T[]
    filterNotNull(T[] val, IntFunction<T[]> arrayConstructor)
    Returns an array with values from val minus null values
    static <T> T
    find(T[] items, Predicate<T> predicate)
    Returns the first element from the array for which condition predicate is true, or null if there is no such element
    static <T> T
    firstOrNull(T[] items)
     
    static <T> T
    getOrNull(T[] items, int i)
    Returns the i-th item in items, if it exists and items is not null, otherwise returns null.
    static <T> int
    indexOf(T[] array, T value)
    Return first index of value in array, or -1 if not found.
    static boolean
    isEmpty(boolean[] array)
    Checks if given array is null or has zero elements.
    static boolean
    isEmpty(byte[] array)
    Checks if given array is null or has zero elements.
    static boolean
    isEmpty(int[] array)
    Checks if given array is null or has zero elements.
    static boolean
    isEmpty(long[] array)
    Checks if given array is null or has zero elements.
    static boolean
    isEmpty(Collection<?> array)
    Checks if given array is null or has zero elements.
    static boolean
    isEmpty(Map<?,?> map)
    Checks if given map is null or has zero elements.
    static <T> boolean
    isEmpty(T[] array)
    Checks if given array is null or has zero elements.
    static <T> T[]
    newUnpaddedArray(Class<T> clazz, int minLen)
     
    static boolean[]
     
    static byte[]
     
    static char[]
     
    static float[]
     
    static int[]
    newUnpaddedIntArray(int minLen)
     
    static long[]
     
    static Object[]
     
    static <T> boolean
    Returns true if the two ArrayLists are equal with respect to the objects they contain.
    static <T> android.util.ArraySet<T>
    remove(android.util.ArraySet<T> cur, T val)
     
    static <T> ArrayList<T>
    remove(ArrayList<T> cur, T val)
     
    static <T> T[]
    removeElement(Class<T> kind, T[] array, T element)
    Removes value from given array if present, providing set-like behavior.
    static int[]
    removeInt(int[] cur, int val)
    Removes value from given array if present, providing set-like behavior.
    static long[]
    removeLong(long[] cur, long val)
    Removes value from given array if present, providing set-like behavior.
    static String[]
    removeString(String[] cur, String val)
    Removes value from given array if present, providing set-like behavior.
    static int
    size(Object[] array)
    Length of the given array or 0 if it's null.
    static int
    size(Collection<?> collection)
    Length of the given collection or 0 if it's null.
    static int
    size(Map<?,?> map)
    Length of the given map or 0 if it's null.
    static boolean
    startsWith(byte[] cur, byte[] val)
     
    static void
    throwsIfOutOfBounds(int len, int offset, int count)
    Throws ArrayIndexOutOfBoundsException if the range is out of bounds.
    static <T> List<T>
    toList(T[] array)
    Creates a List from an array.
    static long
    total(long[] array)
     
    static <T> T[]
    trimToSize(T[] array, int size)
     
    static <T> int
    unstableRemoveIf(ArrayList<T> collection, Predicate<T> predicate)
    Removes elements that match the predicate in an efficient way that alters the order of elements in the collection.

    Methods inherited from class java.lang.Object

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

    • EMPTY_FILE

      public static final File[] EMPTY_FILE
  • Method Details

    • newUnpaddedByteArray

      public static byte[] newUnpaddedByteArray(int minLen)
    • newUnpaddedCharArray

      public static char[] newUnpaddedCharArray(int minLen)
    • newUnpaddedIntArray

      public static int[] newUnpaddedIntArray(int minLen)
    • newUnpaddedBooleanArray

      public static boolean[] newUnpaddedBooleanArray(int minLen)
    • newUnpaddedLongArray

      public static long[] newUnpaddedLongArray(int minLen)
    • newUnpaddedFloatArray

      public static float[] newUnpaddedFloatArray(int minLen)
    • newUnpaddedObjectArray

      public static Object[] newUnpaddedObjectArray(int minLen)
    • newUnpaddedArray

      public static <T> T[] newUnpaddedArray(Class<T> clazz, int minLen)
    • equals

      public static boolean equals(byte[] array1, byte[] array2, int length)
      Checks if the beginnings of two byte arrays are equal.
      Parameters:
      array1 - the first byte array
      array2 - the second byte array
      length - the number of bytes to check
      Returns:
      true if they're equal, false otherwise
    • emptyArray

      public static <T> T[] emptyArray(Class<T> kind)
      Returns an empty array of the specified type. The intent is that it will return the same empty array every time to avoid reallocation, although this is not guaranteed.
    • emptyIfNull

      public static <T> T[] emptyIfNull(T[] items, Class<T> kind)
      Returns the same array or an empty one if it's null.
    • isEmpty

      public static boolean isEmpty(Collection<?> array)
      Checks if given array is null or has zero elements.
    • isEmpty

      public static boolean isEmpty(Map<?,?> map)
      Checks if given map is null or has zero elements.
    • isEmpty

      public static <T> boolean isEmpty(T[] array)
      Checks if given array is null or has zero elements.
    • isEmpty

      public static boolean isEmpty(int[] array)
      Checks if given array is null or has zero elements.
    • isEmpty

      public static boolean isEmpty(long[] array)
      Checks if given array is null or has zero elements.
    • isEmpty

      public static boolean isEmpty(byte[] array)
      Checks if given array is null or has zero elements.
    • isEmpty

      public static boolean isEmpty(boolean[] array)
      Checks if given array is null or has zero elements.
    • size

      public static int size(Object[] array)
      Length of the given array or 0 if it's null.
    • size

      public static int size(Collection<?> collection)
      Length of the given collection or 0 if it's null.
    • size

      public static int size(Map<?,?> map)
      Length of the given map or 0 if it's null.
    • contains

      public static <T> boolean contains(T[] array, T value)
      Checks that value is present as at least one of the elements of the array.
      Parameters:
      array - the array to check in
      value - the value to check for
      Returns:
      true if the value is present in the array
    • indexOf

      public static <T> int indexOf(T[] array, T value)
      Return first index of value in array, or -1 if not found.
    • containsAll

      public static <T> boolean containsAll(T[] array, T[] check)
      Test if all check items are contained in array.
    • containsAny

      public static <T> boolean containsAny(T[] array, T[] check)
      Test if any check items are contained in array.
    • contains

      public static boolean contains(int[] array, int value)
    • contains

      public static boolean contains(long[] array, long value)
    • contains

      public static boolean contains(char[] array, char value)
    • containsAll

      public static <T> boolean containsAll(char[] array, char[] check)
      Test if all check items are contained in array.
    • total

      public static long total(long[] array)
    • convertToIntArray

      @Deprecated public static int[] convertToIntArray(List<Integer> list)
      Deprecated.
      use IntArray instead
    • convertToIntArray

      public static int[] convertToIntArray(android.util.ArraySet<Integer> set)
    • convertToLongArray

      public static long[] convertToLongArray(int[] intArray)
    • concat

      public static <T> T[] concat(Class<T> kind, T[]... arrays)
      Returns the concatenation of the given arrays. Only works for object arrays, not for primitive arrays. See concat(byte[]...) for a variant that works on byte arrays.
      Type Parameters:
      T - The class of the array elements (inferred from kind).
      Parameters:
      kind - The class of the array elements
      arrays - The arrays to concatenate. Null arrays are treated as empty.
      Returns:
      A single array containing all the elements of the parameter arrays.
    • concat

      public static byte[] concat(byte[]... arrays)
      Returns the concatenation of the given byte arrays. Null arrays are treated as empty.
    • appendElement

      public static <T> T[] appendElement(Class<T> kind, T[] array, T element)
      Adds value to given array if not already present, providing set-like behavior.
    • appendElement

      public static <T> T[] appendElement(Class<T> kind, T[] array, T element, boolean allowDuplicates)
      Adds value to given array.
    • removeElement

      public static <T> T[] removeElement(Class<T> kind, T[] array, T element)
      Removes value from given array if present, providing set-like behavior.
    • appendInt

      public static int[] appendInt(int[] cur, int val, boolean allowDuplicates)
      Adds value to given array.
    • appendInt

      public static int[] appendInt(int[] cur, int val)
      Adds value to given array if not already present, providing set-like behavior.
    • removeInt

      public static int[] removeInt(int[] cur, int val)
      Removes value from given array if present, providing set-like behavior.
    • removeString

      public static String[] removeString(String[] cur, String val)
      Removes value from given array if present, providing set-like behavior.
    • appendLong

      public static long[] appendLong(long[] cur, long val, boolean allowDuplicates)
      Adds value to given array if not already present, providing set-like behavior.
    • appendLong

      public static long[] appendLong(long[] cur, long val)
      Adds value to given array if not already present, providing set-like behavior.
    • removeLong

      public static long[] removeLong(long[] cur, long val)
      Removes value from given array if present, providing set-like behavior.
    • cloneOrNull

      public static long[] cloneOrNull(long[] array)
    • cloneOrNull

      public static <T> T[] cloneOrNull(T[] array)
      Clones an array or returns null if the array is null.
    • cloneOrNull

      public static <T> android.util.ArraySet<T> cloneOrNull(android.util.ArraySet<T> array)
    • add

      public static <T> android.util.ArraySet<T> add(android.util.ArraySet<T> cur, T val)
    • addAll

      public static <T> android.util.ArraySet<T> addAll(android.util.ArraySet<T> cur, Collection<T> val)
      Similar to Set.addAll(Collection)}, but with support for set values of null.
    • remove

      public static <T> android.util.ArraySet<T> remove(android.util.ArraySet<T> cur, T val)
    • add

      public static <T> ArrayList<T> add(ArrayList<T> cur, T val)
    • add

      public static <T> ArrayList<T> add(ArrayList<T> cur, int index, T val)
    • remove

      public static <T> ArrayList<T> remove(ArrayList<T> cur, T val)
    • contains

      public static <T> boolean contains(Collection<T> cur, T val)
    • trimToSize

      public static <T> T[] trimToSize(T[] array, int size)
    • referenceEquals

      public static <T> boolean referenceEquals(ArrayList<T> a, ArrayList<T> b)
      Returns true if the two ArrayLists are equal with respect to the objects they contain. The objects must be in the same order and be reference equal (== not .equals()).
    • unstableRemoveIf

      public static <T> int unstableRemoveIf(ArrayList<T> collection, Predicate<T> predicate)
      Removes elements that match the predicate in an efficient way that alters the order of elements in the collection. This should only be used if order is not important.
      Parameters:
      collection - The ArrayList from which to remove elements.
      predicate - The predicate that each element is tested against.
      Returns:
      the number of elements removed.
    • defeatNullable

      public static int[] defeatNullable(int[] val)
    • defeatNullable

      public static String[] defeatNullable(String[] val)
    • defeatNullable

      public static File[] defeatNullable(File[] val)
    • checkBounds

      public static void checkBounds(int len, int index)
      Throws ArrayIndexOutOfBoundsException if the index is out of bounds.
      Parameters:
      len - length of the array. Must be non-negative
      index - the index to check
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out of bounds of the array
    • throwsIfOutOfBounds

      public static void throwsIfOutOfBounds(int len, int offset, int count)
      Throws ArrayIndexOutOfBoundsException if the range is out of bounds.
      Parameters:
      len - length of the array. Must be non-negative
      offset - start index of the range. Must be non-negative
      count - length of the range. Must be non-negative
      Throws:
      ArrayIndexOutOfBoundsException - if the range from offset with length count is out of bounds of the array
    • filterNotNull

      public static <T> T[] filterNotNull(T[] val, IntFunction<T[]> arrayConstructor)
      Returns an array with values from val minus null values
      Parameters:
      arrayConstructor - typically T[]::new e.g. String[]::new
    • filter

      public static <T> T[] filter(T[] items, IntFunction<T[]> arrayConstructor, Predicate<T> predicate)
      Returns an array containing elements from the given one that match the given predicate. The returned array may, in some cases, be the reference to the input array.
    • startsWith

      public static boolean startsWith(byte[] cur, byte[] val)
    • find

      public static <T> T find(T[] items, Predicate<T> predicate)
      Returns the first element from the array for which condition predicate is true, or null if there is no such element
    • deepToString

      public static String deepToString(Object value)
    • getOrNull

      public static <T> T getOrNull(T[] items, int i)
      Returns the i-th item in items, if it exists and items is not null, otherwise returns null.
    • firstOrNull

      public static <T> T firstOrNull(T[] items)
    • toList

      public static <T> List<T> toList(T[] array)
      Creates a List from an array. Different from Arrays.asList(Object[]) as that will use the parameter as the backing array, meaning changes are not isolated.