Class ArrayUtilities


  • public final class ArrayUtilities
    extends Object
    Handy utilities for working with Java arrays.
    Author:
    Ken Partlow, John DeRegnaucourt ([email protected])
    Copyright (c) Cedar Software LLC

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
    • Field Detail

      • EMPTY_OBJECT_ARRAY

        public static final Object[] EMPTY_OBJECT_ARRAY
        Immutable common arrays.
      • EMPTY_CLASS_ARRAY

        public static final Class[] EMPTY_CLASS_ARRAY
    • Method Detail

      • isEmpty

        public static boolean isEmpty​(Object array)
        This is a null-safe isEmpty check. It uses the Array static class for doing a length check. This check is actually .0001 ms slower than the following typed check:

        return array == null || array.length == 0;

        but gives you more flexibility, since it checks for all array types.
        Parameters:
        array - array to check
        Returns:
        true if empty or null
      • size

        public static int size​(Object array)
        This is a null-safe size check. It uses the Array static class for doing a length check. This check is actually .0001 ms slower than the following typed check:

        return (array == null) ? 0 : array.length;

        Parameters:
        array - array to check
        Returns:
        true if empty or null
      • shallowCopy

        public static <T> T[] shallowCopy​(T[] array)

        Shallow copies an array of Objects

        The objects in the array are not cloned, thus there is no special handling for multi-dimensional arrays.

        This method returns null if null array input.

        Type Parameters:
        T - the array type
        Parameters:
        array - the array to shallow clone, may be null
        Returns:
        the cloned array, null if null input
      • addAll

        public static <T> T[] addAll​(T[] array1,
                                     T[] array2)

        Adds all the elements of the given arrays into a new array.

        The new array contains all of the element of array1 followed by all of the elements array2. When an array is returned, it is always a new array.

         ArrayUtilities.addAll(null, null)     = null
         ArrayUtilities.addAll(array1, null)   = cloned copy of array1
         ArrayUtilities.addAll(null, array2)   = cloned copy of array2
         ArrayUtilities.addAll([], [])         = []
         ArrayUtilities.addAll([null], [null]) = [null, null]
         ArrayUtilities.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
         
        Type Parameters:
        T - the array type
        Parameters:
        array1 - the first array whose elements are added to the new array, may be null
        array2 - the second array whose elements are added to the new array, may be null
        Returns:
        The new array, null if null array inputs. The type of the new array is the type of the first array.
      • removeItem

        public static <T> T[] removeItem​(T[] array,
                                         int pos)
      • getArraySubset

        public static <T> T[] getArraySubset​(T[] array,
                                             int start,
                                             int end)
      • toArray

        public static <T> T[] toArray​(Class<T> classToCastTo,
                                      Collection<?> c)
        Convert Collection to a Java (typed) array [].
        Type Parameters:
        T - Type of the array
        Parameters:
        classToCastTo - array type (Object[], Person[], etc.)
        c - Collection containing items to be placed into the array.
        Returns:
        Array of the type (T) containing the items from collection 'c'.