public final class ArrayUtil extends Object
Modifier and Type | Field and Description |
---|---|
static int |
MAX_ARRAY_LENGTH
Maximum length for an array (Integer.MAX_VALUE - RamUsageEstimator.NUM_BYTES_ARRAY_HEADER).
|
Modifier and Type | Method and Description |
---|---|
static byte[] |
copyOfSubArray(byte[] array,
int from,
int to)
Copies the specified range of the given array into a new sub array.
|
static char[] |
copyOfSubArray(char[] array,
int from,
int to)
Copies the specified range of the given array into a new sub array.
|
static double[] |
copyOfSubArray(double[] array,
int from,
int to)
Copies the specified range of the given array into a new sub array.
|
static float[] |
copyOfSubArray(float[] array,
int from,
int to)
Copies the specified range of the given array into a new sub array.
|
static int[] |
copyOfSubArray(int[] array,
int from,
int to)
Copies the specified range of the given array into a new sub array.
|
static long[] |
copyOfSubArray(long[] array,
int from,
int to)
Copies the specified range of the given array into a new sub array.
|
static short[] |
copyOfSubArray(short[] array,
int from,
int to)
Copies the specified range of the given array into a new sub array.
|
static <T> T[] |
copyOfSubArray(T[] array,
int from,
int to)
Copies the specified range of the given array into a new sub array.
|
static byte[] |
grow(byte[] array)
Returns a larger array, generally over-allocating exponentially
|
static byte[] |
grow(byte[] array,
int minSize)
Returns an array whose size is at least
minSize , generally over-allocating exponentially |
static char[] |
grow(char[] array)
Returns a larger array, generally over-allocating exponentially
|
static char[] |
grow(char[] array,
int minSize)
Returns an array whose size is at least
minSize , generally over-allocating exponentially |
static double[] |
grow(double[] array)
Returns a larger array, generally over-allocating exponentially
|
static double[] |
grow(double[] array,
int minSize)
Returns an array whose size is at least
minSize , generally over-allocating exponentially |
static float[] |
grow(float[] array)
Returns a larger array, generally over-allocating exponentially
|
static float[] |
grow(float[] array,
int minSize)
Returns an array whose size is at least
minSize , generally over-allocating exponentially |
static int[] |
grow(int[] array)
Returns a larger array, generally over-allocating exponentially
|
static int[] |
grow(int[] array,
int minSize)
Returns an array whose size is at least
minSize , generally over-allocating exponentially |
static long[] |
grow(long[] array)
Returns a larger array, generally over-allocating exponentially
|
static long[] |
grow(long[] array,
int minSize)
Returns an array whose size is at least
minSize , generally over-allocating exponentially |
static short[] |
grow(short[] array)
Returns a larger array, generally over-allocating exponentially
|
static short[] |
grow(short[] array,
int minSize)
Returns an array whose size is at least
minSize , generally over-allocating exponentially |
static <T> T[] |
grow(T[] array,
int minSize)
Returns an array whose size is at least
minSize , generally over-allocating exponentially |
static byte[] |
growExact(byte[] array,
int newLength)
Returns a new array whose size is exact the specified
newLength without over-allocating |
static char[] |
growExact(char[] array,
int newLength)
Returns a new array whose size is exact the specified
newLength without over-allocating |
static double[] |
growExact(double[] array,
int newLength)
Returns a new array whose size is exact the specified
newLength without over-allocating |
static float[] |
growExact(float[] array,
int newLength)
Returns a new array whose size is exact the specified
newLength without over-allocating |
static int[] |
growExact(int[] array,
int newLength)
Returns a new array whose size is exact the specified
newLength without over-allocating |
static long[] |
growExact(long[] array,
int newLength)
Returns a new array whose size is exact the specified
newLength without over-allocating |
static short[] |
growExact(short[] array,
int newLength)
Returns a new array whose size is exact the specified
newLength without over-allocating |
static <T> T[] |
growExact(T[] array,
int newLength)
Returns a new array whose size is exact the specified
newLength without over-allocating |
static int |
hashCode(char[] array,
int start,
int end)
Returns hash of chars in range start (inclusive) to
end (inclusive)
|
static <T extends Comparable<? super T>> |
introSort(T[] a)
Sorts the given array in natural order.
|
static <T> void |
introSort(T[] a,
Comparator<? super T> comp)
Sorts the given array using the
Comparator . |
static <T extends Comparable<? super T>> |
introSort(T[] a,
int fromIndex,
int toIndex)
Sorts the given array slice in natural order.
|
static <T> void |
introSort(T[] a,
int fromIndex,
int toIndex,
Comparator<? super T> comp)
Sorts the given array slice using the
Comparator . |
static int |
oversize(int minTargetSize,
int bytesPerElement)
Returns an array size >= minTargetSize, generally
over-allocating exponentially to achieve amortized
linear-time cost as the array grows.
|
static int |
parseInt(char[] chars,
int offset,
int len)
Parses a char array into an int.
|
static int |
parseInt(char[] chars,
int offset,
int len,
int radix)
Parses the string argument as if it was an int value and returns the
result.
|
static <T> void |
select(T[] arr,
int from,
int to,
int k,
Comparator<? super T> comparator)
Reorganize
arr[from:to[ so that the element at offset k is at the
same position as if arr[from:to] was sorted, and all elements on
its left are less than or equal to it, and all elements on its right are
greater than or equal to it. |
static <T> void |
swap(T[] arr,
int i,
int j)
Swap values stored in slots
i and j |
static <T extends Comparable<? super T>> |
timSort(T[] a)
Sorts the given array in natural order.
|
static <T> void |
timSort(T[] a,
Comparator<? super T> comp)
Sorts the given array using the
Comparator . |
static <T extends Comparable<? super T>> |
timSort(T[] a,
int fromIndex,
int toIndex)
Sorts the given array slice in natural order.
|
static <T> void |
timSort(T[] a,
int fromIndex,
int toIndex,
Comparator<? super T> comp)
Sorts the given array slice using the
Comparator . |
public static final int MAX_ARRAY_LENGTH
public static int parseInt(char[] chars, int offset, int len) throws NumberFormatException
chars
- the character arrayoffset
- The offset into the arraylen
- The lengthNumberFormatException
- if it can't parsepublic static int parseInt(char[] chars, int offset, int len, int radix) throws NumberFormatException
chars
- a string representation of an int quantity.radix
- the base to use for conversion.NumberFormatException
- if the argument could not be parsed as an int quantity.public static int oversize(int minTargetSize, int bytesPerElement)
minTargetSize
- Minimum required value to be returned.bytesPerElement
- Bytes used by each element of
the array. See constants in RamUsageEstimator
.public static <T> T[] growExact(T[] array, int newLength)
newLength
without over-allocatingpublic static <T> T[] grow(T[] array, int minSize)
minSize
, generally over-allocating exponentiallypublic static short[] growExact(short[] array, int newLength)
newLength
without over-allocatingpublic static short[] grow(short[] array, int minSize)
minSize
, generally over-allocating exponentiallypublic static short[] grow(short[] array)
public static float[] growExact(float[] array, int newLength)
newLength
without over-allocatingpublic static float[] grow(float[] array, int minSize)
minSize
, generally over-allocating exponentiallypublic static float[] grow(float[] array)
public static double[] growExact(double[] array, int newLength)
newLength
without over-allocatingpublic static double[] grow(double[] array, int minSize)
minSize
, generally over-allocating exponentiallypublic static double[] grow(double[] array)
public static int[] growExact(int[] array, int newLength)
newLength
without over-allocatingpublic static int[] grow(int[] array, int minSize)
minSize
, generally over-allocating exponentiallypublic static int[] grow(int[] array)
public static long[] growExact(long[] array, int newLength)
newLength
without over-allocatingpublic static long[] grow(long[] array, int minSize)
minSize
, generally over-allocating exponentiallypublic static long[] grow(long[] array)
public static byte[] growExact(byte[] array, int newLength)
newLength
without over-allocatingpublic static byte[] grow(byte[] array, int minSize)
minSize
, generally over-allocating exponentiallypublic static byte[] grow(byte[] array)
public static char[] growExact(char[] array, int newLength)
newLength
without over-allocatingpublic static char[] grow(char[] array, int minSize)
minSize
, generally over-allocating exponentiallypublic static char[] grow(char[] array)
public static int hashCode(char[] array, int start, int end)
public static <T> void swap(T[] arr, int i, int j)
i
and j
public static <T> void introSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp)
Comparator
. This method uses the intro sort
algorithm, but falls back to insertion sort for small arrays.fromIndex
- start index (inclusive)toIndex
- end index (exclusive)public static <T> void introSort(T[] a, Comparator<? super T> comp)
Comparator
. This method uses the intro sort
algorithm, but falls back to insertion sort for small arrays.public static <T extends Comparable<? super T>> void introSort(T[] a, int fromIndex, int toIndex)
fromIndex
- start index (inclusive)toIndex
- end index (exclusive)public static <T extends Comparable<? super T>> void introSort(T[] a)
public static <T> void timSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> comp)
Comparator
. This method uses the Tim sort
algorithm, but falls back to binary sort for small arrays.fromIndex
- start index (inclusive)toIndex
- end index (exclusive)public static <T> void timSort(T[] a, Comparator<? super T> comp)
Comparator
. This method uses the Tim sort
algorithm, but falls back to binary sort for small arrays.public static <T extends Comparable<? super T>> void timSort(T[] a, int fromIndex, int toIndex)
fromIndex
- start index (inclusive)toIndex
- end index (exclusive)public static <T extends Comparable<? super T>> void timSort(T[] a)
public static <T> void select(T[] arr, int from, int to, int k, Comparator<? super T> comparator)
arr[from:to[
so that the element at offset k is at the
same position as if arr[from:to]
was sorted, and all elements on
its left are less than or equal to it, and all elements on its right are
greater than or equal to it.
This runs in linear time on average and in n log(n)
time in the
worst case.arr
- Array to be re-organized.from
- Starting index for re-organization. Elements before this index
will be left as is.to
- Ending index. Elements after this index will be left as is.k
- Index of element to sort from. Value must be less than 'to' and greater than or equal to 'from'.comparator
- Comparator to use for sortingpublic static byte[] copyOfSubArray(byte[] array, int from, int to)
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)public static char[] copyOfSubArray(char[] array, int from, int to)
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)public static short[] copyOfSubArray(short[] array, int from, int to)
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)public static int[] copyOfSubArray(int[] array, int from, int to)
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)public static long[] copyOfSubArray(long[] array, int from, int to)
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)public static float[] copyOfSubArray(float[] array, int from, int to)
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)public static double[] copyOfSubArray(double[] array, int from, int to)
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)public static <T> T[] copyOfSubArray(T[] array, int from, int to)
array
- the input arrayfrom
- the initial index of range to be copied (inclusive)to
- the final index of range to be copied (exclusive)Copyright © 2000-2021 Apache Software Foundation. All Rights Reserved.