public final class ArrayTools
extends java.lang.Object
Constructor and Description |
---|
ArrayTools() |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object[] |
concatArrays(java.lang.Object[] a1,
java.lang.Object[] a2)
Concatenates two arrays into a new array, handling null values.
|
static <R,A extends R,B extends R> |
concatTypedArrays(java.lang.Class<R> componentType,
A[] a1,
B[] a2)
Concatenates two typed arrays into a new array, handling null values.
|
static int |
findInsertionIndex(int[] sortedArray,
IntIndexComparator comparator,
int valueToInsert)
Finds the index at which an additional value needs to be inserted into a sorted integer array
to maintain the sorted order according to the given comparator.
|
static int |
findInsertionIndex(int[] sortedArray,
int length,
IntIndexComparator comparator,
int valueToInsert)
Finds the index at which an additional value needs to be inserted into a sorted integer array
to maintain the sorted order according to the given comparator, within a specified effective length.
|
static int |
lastIndexOf(byte[] what,
byte[] within)
Finds the last occurrence of the specified byte array within the given byte array,
searching backward from the end.
|
static int |
lastIndexOf(byte[] what,
byte[] within,
int fromIndex)
Finds the last occurrence of the specified byte array within the given byte array,
searching backward from the specified index.
|
static java.lang.Object[] |
mergeArrays(java.lang.Object[] a1,
java.lang.Object[] a2)
Merges two arrays of objects into a new array with duplicates removed,
preserving the order of the first occurrence of each element.
|
static <R,A extends R,B extends R> |
mergeTypedArrays(java.lang.Class<R> componentType,
A[] a1,
B[] a2)
Merges two typed arrays into a new array with duplicates removed, preserving the order of the first occurrence of each element.
|
static java.lang.Object[] |
removeArrayMemberAtIndex(java.lang.Object[] a,
int i)
Returns a new
Object array without the element at the specified position,
by copying the given Object array into a new Object array without
the element at the specified position. |
static java.lang.Object |
removeTypedArrayMemberAtIndex(java.lang.Object a,
int i)
Similar to
removeArrayMemberAtIndex(java.lang.Object[], int) , but with Object parameter and
return type to be casted to the correct array type. |
public static java.lang.Object[] concatArrays(java.lang.Object[] a1, java.lang.Object[] a2)
This method combines the elements of two arrays (a1 and a2) into a new array. If both input arrays are null, an empty array is returned. The order of the elements in the new array is the same as in the input arrays: elements from a1 first, followed by elements from a2.
a1
- The first array. May be null.a2
- The second array. May be null.public static <R,A extends R,B extends R> R[] concatTypedArrays(java.lang.Class<R> componentType, A[] a1, B[] a2)
This method combines the elements of two typed arrays (a1 and a2) into a new array. If both input arrays are null, an empty array of the specified component type is returned. The order of the elements in the new array is the same as in the input arrays: elements from a1 first, followed by elements from a2.
R
- The type of the elements in the resulting array.A
- The type of the elements in the first array, which must be a subtype of R.B
- The type of the elements in the second array, which must be a subtype of R.componentType
- The component type of the resulting array.a1
- The first array. May be null.a2
- The second array. May be null.public static final java.lang.Object[] mergeArrays(java.lang.Object[] a1, java.lang.Object[] a2)
a1
- the first arraya2
- the second arraypublic static <R,A extends R,B extends R> R[] mergeTypedArrays(java.lang.Class<R> componentType, A[] a1, B[] a2)
This method combines the elements of two arrays (a1 and a2) into a new array, ensuring that only unique elements are included. The order of the elements in the new array is determined by their first occurrence in either of the input arrays. The component type of the arrays is specified by the componentType parameter.
R
- The type of the elements in the arrays.A
- The type of the first array, which must be a subtype of R.B
- The type of the second array, which must be a subtype of R.componentType
- The component type of the arrays.a1
- The first array.a2
- The second array.public static final int lastIndexOf(byte[] what, byte[] within)
what
- The byte array to search for.within
- The byte array to search within.public static final int lastIndexOf(byte[] what, byte[] within, int fromIndex)
what
- The byte array to search for.within
- The byte array to search within.fromIndex
- The index to start the backward search from.public static final java.lang.Object[] removeArrayMemberAtIndex(java.lang.Object[] a, int i)
Object
array without the element at the specified position,
by copying the given Object
array into a new Object
array without
the element at the specified position. The new Object
array will have a length
of a.length - 1
, and the original array will be left untouched.a
- The Object
array to remove the element from.i
- The element position to remove.Object
array without the element at the specified position.java.lang.IllegalArgumentException
- If the given array is null.java.lang.ArrayIndexOutOfBoundsException
- If the given element position is out of bounds.public static final java.lang.Object removeTypedArrayMemberAtIndex(java.lang.Object a, int i)
removeArrayMemberAtIndex(java.lang.Object[], int)
, but with Object
parameter and
return type to be casted to the correct array type.a
- The Object
array to remove the element from.i
- The element position to remove.Object
array without the element at the specified position.java.lang.IllegalArgumentException
- If the given array is null.java.lang.ArrayIndexOutOfBoundsException
- If the given element position is out of bounds.public static final int findInsertionIndex(int[] sortedArray, IntIndexComparator comparator, int valueToInsert)
This method performs a binary search to find the first index "i" such that
comparator.compare(valueToInsert, sortedArray[i]) <= 0
. If no such element exists
(i.e. valueToInsert
is greater than all elements in the array), it returns
the array's length, indicating insertion at the end.
sortedArray
- The array, which must be sorted according to the provided comparator.
If null, an IllegalArgumentException
is thrown.comparator
- The IntIndexComparator
used to define the sorting order of the array.
If null, an IllegalArgumentException
is thrown.valueToInsert
- The integer value to find the insertion index for.java.lang.IllegalArgumentException
- if sortedArray
or comparator
is null.public static final int findInsertionIndex(int[] sortedArray, int length, IntIndexComparator comparator, int valueToInsert)
This method performs a binary search to find the first index "i" such that
comparator.compare(valueToInsert, sortedArray[i]) <= 0
. If no such element exists
within the specified "length" (i.e. valueToInsert
is greater than all elements
up to "length - 1"), it returns "length", indicating insertion at the end of the effective range.
sortedArray
- The array, which must be sorted according to the provided comparator.
If null, an IllegalArgumentException
is thrown.length
- The effective length of the sorted portion of the array to consider for the binary search.
Must not be larger than the length of the passed "sortedArray".comparator
- The IntIndexComparator
used to define the sorting order of the array.
If null, an IllegalArgumentException
is thrown.valueToInsert
- The integer value to find the insertion index for.java.lang.IllegalArgumentException
- if sortedArray
or comparator
is null,
or if "length" is greater than "sortedArray.length".Copyright © 2000-2025 OAshi S.à r.l. All Rights Reserved.