Class ArrayUtilities
java.lang.Object
com.cedarsoftware.util.ArrayUtilities
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
License
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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]
addAll
(T[] array1, T[] array2) Adds all the elements of the given arrays into a new array.static <T> T[]
createArray
(T... elements) Creates and returns an array containing the provided elements.static <T> T[]
getArraySubset
(T[] array, int start, int end) static boolean
This is a null-safe isEmpty check.static <T> T[]
removeItem
(T[] array, int pos) static <T> T[]
shallowCopy
(T[] array) Shallow copies an array of Objectsstatic int
This is a null-safe size check.static <T> T[]
toArray
(Class<T> classToCastTo, Collection<?> c) Convert Collection to a Java (typed) array [].
-
Field Details
-
EMPTY_OBJECT_ARRAY
Immutable common arrays. -
EMPTY_BYTE_ARRAY
public static final byte[] EMPTY_BYTE_ARRAY -
EMPTY_CHAR_ARRAY
public static final char[] EMPTY_CHAR_ARRAY -
EMPTY_CHARACTER_ARRAY
-
EMPTY_CLASS_ARRAY
-
-
Method Details
-
isEmpty
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;
- Parameters:
array
- array to check- Returns:
- true if empty or null
-
size
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
ifnull
array input.- Type Parameters:
T
- the array type- Parameters:
array
- the array to shallow clone, may benull
- Returns:
- the cloned array,
null
ifnull
input
-
createArray
Creates and returns an array containing the provided elements.This method accepts a variable number of arguments and returns them as an array of type
T[]
. It is primarily used to facilitate array creation in generic contexts, where type inference is necessary.Example Usage:
String[] stringArray = createArray("Apple", "Banana", "Cherry"); Integer[] integerArray = createArray(1, 2, 3, 4); Person[] personArray = createArray(new Person("Alice"), new Person("Bob"));
Important Considerations:
- Type Safety: Due to type erasure in Java generics, this method does not perform any type checks
beyond what is already enforced by the compiler. Ensure that all elements are of the expected type
T
to avoidClassCastException
at runtime. - Heap Pollution: The method is annotated with
SafeVarargs
to suppress warnings related to heap pollution when using generics with varargs. It is safe to use because the method does not perform any unsafe operations on the varargs parameter. - Null Elements: The method does not explicitly handle
null
elements. Ifnull
values are passed, they will be included in the returned array. - Immutable Arrays: The returned array is mutable. To create an immutable array, consider wrapping it
using
Collections.unmodifiableList(List)
or using third-party libraries like Guava'sinvalid reference
com.google.common.collect.ImmutableList
- Type Parameters:
T
- the component type of the array- Parameters:
elements
- the elements to be stored in the array- Returns:
- an array containing the provided elements
- Throws:
NullPointerException
- if theelements
array isnull
- Type Safety: Due to type erasure in Java generics, this method does not perform any type checks
beyond what is already enforced by the compiler. Ensure that all elements are of the expected type
-
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 elementsarray2
. 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 benull
array2
- the second array whose elements are added to the new array, may benull
- Returns:
- The new array,
null
ifnull
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
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'.
-