public class ObjectArrays extends Object
ensureCapacity()
, grow()
,
trim()
and setLength()
methods allow to handle
arrays much like array lists. This can be very useful when efficiency (or
syntactic simplicity) reasons make array lists unsuitable.
Warning: creating arrays
using reflection, as it
happens in ensureCapacity(Object[],int,int)
and grow(Object[],int,int)
,
is significantly slower than using new
. This phenomenon is particularly
evident in the first growth phases of an array reallocated with doubling (or similar) logic.
Arrays
Modifier and Type | Field and Description |
---|---|
static Object[] |
EMPTY_ARRAY
A static, final, empty array.
|
static Hash.Strategy |
HASH_STRATEGY
A type-specific content-based hash strategy for arrays.
|
Modifier and Type | Method and Description |
---|---|
static <K> int |
binarySearch(K[] a,
int from,
int to,
K key)
Searches a range of the specified array for the specified value using
the binary search algorithm.
|
static <K> int |
binarySearch(K[] a,
int from,
int to,
K key,
Comparator<K> c)
Searches a range of the specified array for the specified value using
the binary search algorithm and a specified comparator.
|
static <K> int |
binarySearch(K[] a,
K key)
Searches an array for the specified value using
the binary search algorithm.
|
static <K> int |
binarySearch(K[] a,
K key,
Comparator<K> c)
Searches an array for the specified value using
the binary search algorithm and a specified comparator.
|
static <K> K[] |
copy(K[] array)
Returns a copy of an array.
|
static <K> K[] |
copy(K[] array,
int offset,
int length)
Returns a copy of a portion of an array.
|
static <K> K[] |
ensureCapacity(K[] array,
int length)
Ensures that an array can contain the given number of entries.
|
static <K> K[] |
ensureCapacity(K[] array,
int length,
int preserve)
Ensures that an array can contain the given number of entries, preserving just a part of the array.
|
static <K> void |
ensureFromTo(K[] a,
int from,
int to)
Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array.
|
static <K> void |
ensureOffsetLength(K[] a,
int offset,
int length)
Ensures that a range given by an offset and a length fits an array.
|
static <K> boolean |
equals(K[] a1,
K[] a2)
Deprecated.
Please use the corresponding
Arrays method, which is intrinsified in recent JVMs. |
static <K> void |
fill(K[] array,
int from,
int to,
K value)
Fills a portion of the given array with the given value.
|
static <K> void |
fill(K[] array,
K value)
Fills the given array with the given value.
|
static <K> K[] |
grow(K[] array,
int length)
Grows the given array to the maximum between the given length and
the current length multiplied by two, provided that the given
length is larger than the current length.
|
static <K> K[] |
grow(K[] array,
int length,
int preserve)
Grows the given array to the maximum between the given length and
the current length multiplied by two, provided that the given
length is larger than the current length, preserving just a part of the array.
|
static <K> void |
mergeSort(K[] a)
Sorts an array according to the natural ascending order using mergesort.
|
static <K> void |
mergeSort(K[] a,
Comparator<K> comp)
Sorts an array according to the order induced by the specified
comparator using mergesort.
|
static <K> void |
mergeSort(K[] a,
int from,
int to)
Sorts the specified range of elements according to the natural ascending order using mergesort.
|
static <K> void |
mergeSort(K[] a,
int from,
int to,
Comparator<K> comp)
Sorts the specified range of elements according to the order induced by the specified
comparator using mergesort.
|
static <K> void |
mergeSort(K[] a,
int from,
int to,
Comparator<K> comp,
K[] supp)
Sorts the specified range of elements according to the order induced by the specified
comparator using mergesort, using a given support array.
|
static <K> void |
mergeSort(K[] a,
int from,
int to,
K[] supp)
Sorts the specified range of elements according to the natural ascending order using mergesort, using a given support array.
|
static <K> void |
quickSort(K[] x)
Deprecated.
Use the corresponding
sort() method in Arrays . |
static <K> void |
quickSort(K[] x,
Comparator<K> comp)
Sorts an array according to the order induced by the specified
comparator using quicksort.
|
static <K> void |
quickSort(K[] x,
int from,
int to)
Deprecated.
Use the corresponding
sort() method in Arrays . |
static <K> void |
quickSort(K[] x,
int from,
int to,
Comparator<K> comp)
Sorts the specified range of elements according to the order induced by the specified
comparator using quicksort.
|
static <K> K[] |
reverse(K[] a)
Reverses the order of the elements in the specified array.
|
static <K> K[] |
setLength(K[] array,
int length)
Sets the length of the given array.
|
static <K> K[] |
shuffle(K[] a,
int from,
int to,
Random random)
Shuffles the specified array fragment using the specified pseudorandom number generator.
|
static <K> K[] |
shuffle(K[] a,
Random random)
Shuffles the specified array using the specified pseudorandom number generator.
|
static <K> K[] |
trim(K[] array,
int length)
Trims the given array to the given length.
|
public static final Object[] EMPTY_ARRAY
public static final Hash.Strategy HASH_STRATEGY
This hash strategy may be used in custom hash collections whenever keys are
arrays, and they must be considered equal by content. This strategy
will handle null
correctly, and it is serializable.
public static <K> K[] ensureCapacity(K[] array, int length)
If you cannot foresee whether this array will need again to be
enlarged, you should probably use grow()
instead.
array
- an array.length
- the new minimum length for this array.array
, if it contains length
entries or more; otherwise,
an array with length
entries whose first array.length
entries are the same as those of array
.public static <K> K[] ensureCapacity(K[] array, int length, int preserve)
array
- an array.length
- the new minimum length for this array.preserve
- the number of elements of the array that must be preserved in case a new allocation is necessary.array
, if it can contain length
entries or more; otherwise,
an array with length
entries whose first preserve
entries are the same as those of array
.public static <K> K[] grow(K[] array, int length)
If you want complete control on the array growth, you
should probably use ensureCapacity()
instead.
array
- an array.length
- the new minimum length for this array.array
, if it can contain length
entries; otherwise, an array with
max(length
,array.length
/φ) entries whose first
array.length
entries are the same as those of array
.public static <K> K[] grow(K[] array, int length, int preserve)
If you want complete control on the array growth, you
should probably use ensureCapacity()
instead.
array
- an array.length
- the new minimum length for this array.preserve
- the number of elements of the array that must be preserved in case a new allocation is necessary.array
, if it can contain length
entries; otherwise, an array with
max(length
,array.length
/φ) entries whose first
preserve
entries are the same as those of array
.public static <K> K[] trim(K[] array, int length)
array
- an array.length
- the new maximum length for the array.array
, if it contains length
entries or less; otherwise, an array with
length
entries whose entries are the same as
the first length
entries of array
.public static <K> K[] setLength(K[] array, int length)
array
- an array.length
- the new length for the array.array
, if it contains exactly length
entries; otherwise, if it contains more than
length
entries, an array with length
entries
whose entries are the same as the first length
entries of
array
; otherwise, an array with length
entries
whose first array.length
entries are the same as those of
array
.public static <K> K[] copy(K[] array, int offset, int length)
array
- an array.offset
- the first element to copy.length
- the number of elements to copy.length
elements of array
starting at offset
.public static <K> K[] copy(K[] array)
array
- an array.array
.public static <K> void fill(K[] array, K value)
This method uses a backward loop. It is significantly faster than the corresponding
method in Arrays
.
array
- an array.value
- the new value for all elements of the array.public static <K> void fill(K[] array, int from, int to, K value)
If possible (i.e., from
is 0) this method uses a
backward loop. In this case, it is significantly faster than the
corresponding method in Arrays
.
array
- an array.from
- the starting index of the portion to fill (inclusive).to
- the end index of the portion to fill (exclusive).value
- the new value for all elements of the specified portion of the array.@Deprecated public static <K> boolean equals(K[] a1, K[] a2)
Arrays
method, which is intrinsified in recent JVMs.a1
- an array.a2
- another array.public static <K> void ensureFromTo(K[] a, int from, int to)
This method may be used whenever an array range check is needed.
a
- an array.from
- a start index (inclusive).to
- an end index (exclusive).IllegalArgumentException
- if from
is greater than to
.ArrayIndexOutOfBoundsException
- if from
or to
are greater than the array length or negative.public static <K> void ensureOffsetLength(K[] a, int offset, int length)
This method may be used whenever an array range check is needed.
a
- an array.offset
- a start index.length
- a length (the number of elements in the range).IllegalArgumentException
- if length
is negative.ArrayIndexOutOfBoundsException
- if offset
is negative or offset
+length
is greater than the array length.public static <K> void quickSort(K[] x, int from, int to, Comparator<K> comp)
The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
x
- the array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.comp
- the comparator to determine the sorting order.public static <K> void quickSort(K[] x, Comparator<K> comp)
The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
x
- the array to be sorted.comp
- the comparator to determine the sorting order.@Deprecated public static <K> void quickSort(K[] x, int from, int to)
The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
x
- the array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.@Deprecated public static <K> void quickSort(K[] x)
The sorting algorithm is a tuned quicksort adapted from Jon L. Bentley and M. Douglas McIlroy, “Engineering a Sort Function”, Software: Practice and Experience, 23(11), pages 1249−1265, 1993.
x
- the array to be sorted.public static <K> void mergeSort(K[] a, int from, int to, K[] supp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. Moreover, no support arrays will be allocated.
a
- the array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.supp
- a support array containing at least to
elements.public static <K> void mergeSort(K[] a, int from, int to)
This sort is guaranteed to be stable: equal elements will not be reordered as a result
of the sort. An array as large as a
will be allocated by this method.
a
- the array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.public static <K> void mergeSort(K[] a)
This sort is guaranteed to be stable: equal elements will not be reordered as a result
of the sort. An array as large as a
will be allocated by this method.
a
- the array to be sorted.public static <K> void mergeSort(K[] a, int from, int to, Comparator<K> comp, K[] supp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. Moreover, no support arrays will be allocated.
a
- the array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.comp
- the comparator to determine the sorting order.supp
- a support array containing at least to
elements.public static <K> void mergeSort(K[] a, int from, int to, Comparator<K> comp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result
of the sort. An array as large as a
will be allocated by this method.
a
- the array to be sorted.from
- the index of the first element (inclusive) to be sorted.to
- the index of the last element (exclusive) to be sorted.comp
- the comparator to determine the sorting order.public static <K> void mergeSort(K[] a, Comparator<K> comp)
This sort is guaranteed to be stable: equal elements will not be reordered as a result
of the sort. An array as large as a
will be allocated by this method.
a
- the array to be sorted.comp
- the comparator to determine the sorting order.public static <K> int binarySearch(K[] a, int from, int to, K key)
a
- the array to be searched.from
- the index of the first element (inclusive) to be searched.to
- the index of the last element (exclusive) to be searched.key
- the value to be searched for.Arrays
public static <K> int binarySearch(K[] a, K key)
a
- the array to be searched.key
- the value to be searched for.Arrays
public static <K> int binarySearch(K[] a, int from, int to, K key, Comparator<K> c)
a
- the array to be searched.from
- the index of the first element (inclusive) to be searched.to
- the index of the last element (exclusive) to be searched.key
- the value to be searched for.c
- a comparator.Arrays
public static <K> int binarySearch(K[] a, K key, Comparator<K> c)
a
- the array to be searched.key
- the value to be searched for.c
- a comparator.Arrays
public static <K> K[] shuffle(K[] a, int from, int to, Random random)
a
- the array to be shuffled.from
- the index of the first element (inclusive) to be shuffled.to
- the index of the last element (exclusive) to be shuffled.random
- a pseudorandom number generator (please use a XorShift* generator).a
.public static <K> K[] shuffle(K[] a, Random random)
a
- the array to be shuffled.random
- a pseudorandom number generator (please use a XorShift* generator).a
.public static <K> K[] reverse(K[] a)
a
- the array to be reversed.a
.