Class ObjectArrays
- Since:
- 2.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static Object
checkElementNotNull
(Object element, int index) (package private) static Object[]
checkElementsNotNull
(Object... array) (package private) static Object[]
checkElementsNotNull
(Object[] array, int length) static <T> T[]
concat
(T[] array, T element) Returns a new array that appendselement
toarray
.static <T> T[]
Returns a new array that contains the concatenated contents of two arrays.static <T> T[]
concat
(T element, T[] array) Returns a new array that prependselement
toarray
.(package private) static Object[]
copyAsObjectArray
(Object[] elements, int offset, int length) Returns a copy of the specified subrange of the specified array that is literally an Object[], and not e.g.private static Object[]
static <T> T[]
Returns a new array of the given length with the specified component type.static <T> T[]
newArray
(T[] reference, int length) Returns a new array of the given length with the same type as a reference array.(package private) static void
Swapsarray[i]
witharray[j]
.(package private) static <T> T[]
toArrayImpl
(Object[] src, int offset, int len, T[] dst) Implementation ofCollection.toArray(Object[])
for collections backed by an object array.(package private) static Object[]
toArrayImpl
(Collection<?> c) Returns an array containing all of the elements in the specified collection.(package private) static <T> T[]
toArrayImpl
(Collection<?> c, T[] array) Returns an array containing all of the elements in the specified collection; the runtime type of the returned array is that of the specified array.
-
Constructor Details
-
ObjectArrays
private ObjectArrays()
-
-
Method Details
-
newArray
Returns a new array of the given length with the specified component type.- Parameters:
type
- the component typelength
- the length of the new array
-
newArray
public static <T> T[] newArray(T[] reference, int length) Returns a new array of the given length with the same type as a reference array.- Parameters:
reference
- any array of the desired typelength
- the length of the new array
-
concat
Returns a new array that contains the concatenated contents of two arrays.- Parameters:
first
- the first array of elements to concatenatesecond
- the second array of elements to concatenatetype
- the component type of the returned array
-
concat
public static <T> T[] concat(T element, T[] array) Returns a new array that prependselement
toarray
.- Parameters:
element
- the element to prepend to the front ofarray
array
- the array of elements to append- Returns:
- an array whose size is one larger than
array
, withelement
occupying the first position, and the elements ofarray
occupying the remaining elements.
-
concat
public static <T> T[] concat(T[] array, T element) Returns a new array that appendselement
toarray
.- Parameters:
array
- the array of elements to prependelement
- the element to append to the end- Returns:
- an array whose size is one larger than
array
, with the same contents asarray
, pluselement
occupying the last position.
-
toArrayImpl
Returns an array containing all of the elements in the specified collection; the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the specified collection.If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), the element in the array immediately following the end of the collection is set to
null
. This is useful in determining the length of the collection only if the caller knows that the collection does not contain any null elements.This method returns the elements in the order they are returned by the collection's iterator.
TODO(kevinb): support concurrently modified collections?
- Parameters:
c
- the collection for which to return an array of elementsarray
- the array in which to place the collection elements- Throws:
ArrayStoreException
- if the runtime type of the specified array is not a supertype of the runtime type of every element in the specified collection
-
toArrayImpl
Implementation ofCollection.toArray(Object[])
for collections backed by an object array. the runtime type of the returned array is that of the specified array. If the collection fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of the specified collection.If the collection fits in the specified array with room to spare (i.e., the array has more elements than the collection), the element in the array immediately following the end of the collection is set to
null
. This is useful in determining the length of the collection only if the caller knows that the collection does not contain any null elements. -
toArrayImpl
Returns an array containing all of the elements in the specified collection. This method returns the elements in the order they are returned by the collection's iterator. The returned array is "safe" in that no references to it are maintained by the collection. The caller is thus free to modify the returned array.This method assumes that the collection size doesn't change while the method is running.
TODO(kevinb): support concurrently modified collections?
- Parameters:
c
- the collection for which to return an array of elements
-
copyAsObjectArray
Returns a copy of the specified subrange of the specified array that is literally an Object[], and not e.g. aString[]
. -
fillArray
-
swap
Swapsarray[i]
witharray[j]
. -
checkElementsNotNull
-
checkElementsNotNull
-
checkElementNotNull
-