public final class ArrayTools
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static int[] |
add(int[] array,
int element)
Adds an element in an array.
|
static int[] |
add(int[] array,
int element,
int pointer)
Adds an element in an array.
|
static <T> T[] |
add(T[] array,
T element)
Adds an element in an array.
|
static <T> T[] |
add(T[] array,
T[] other)
Appends an array to another.
|
static <T> T[] |
add(T[] array,
T element,
int pointer)
Adds an element in an array.
|
static void |
checkArrayBounds(int arrayLength,
int offset,
int length)
Checks the given range is included in the array (defined by its length).
|
static void |
checkBounds(byte[] bytes,
int offset,
int length)
Checks the given range is included in the given byte array.
|
static void |
checkBounds(int arrayLength,
int offset,
int length)
Checks the given range is included in the array (defined by its length).
|
static void |
checkStringBounds(int stringLength,
int offset,
int length)
Checks the given range is included in the array (defined by its length).
|
static <T> boolean |
contains(T[] array,
T element)
Gets whether an array contains an element or not.
|
static <T> boolean |
contains(T[] array,
T element,
int pointer)
Gets whether an array contains an element or not.
|
static <T> boolean |
containsEquals(T[] array,
T element)
Gets whether an array contains an element or not.
|
static <T> boolean |
containsEquals(T[] array,
T element,
int pointer)
Gets whether an array contains an element or not.
|
static int[] |
copy(int[] array)
Copies an array.
|
static int[] |
copy(int[] array,
int pointer)
Copy the
(ptr + 1) elements of an array. |
static <T> T[] |
copy(java.lang.Object[] array,
java.lang.Class<T[]> type)
Copies an array into an array of a different type.
|
static <T> T[] |
copy(java.lang.Object[] array,
int pointer,
java.lang.Class<T[]> type)
Allocates a new array of the given type, and then copy the first
(pointer + 1) elements of an array. |
static <T> T[] |
copy(T[] array)
Copies an array.
|
static <T> T[] |
copy(T[] array,
int pointer)
Allocates a new array of the same type than the given array, and then copy the first
(pointer + 1)
elements of an array. |
static <T> T[] |
createNewArray(java.lang.Class<T[]> type,
int length)
Creates a new array from the given type and length.
|
static <T> T[] |
createNewArray(T[] array,
int length)
Creates a new array with the same type as the given array.
|
static <T> int |
getIndex(T[] array,
T element)
Gets the index of an element in an array.
|
static <T> int |
getIndex(T[] array,
T element,
int pointer)
Gets the index of an element in an array.
|
static <T> int |
getIndexEquals(T[] array,
T element)
Gets the index of an element in an array.
|
static <T> int |
getIndexEquals(T[] array,
T element,
int pointer)
Gets the index of an element in an array.
|
static <T> T[] |
insert(T[] array,
int index,
T element)
Inserts an element at an index in an array.
|
static <T> T[] |
insert(T[] array,
int index,
T element,
int pointer)
Inserts an element at an index in an array.
|
static int[] |
remove(int[] array,
int element)
Removes an element in an array.
|
static boolean |
remove(int[] array,
int element,
int pointer)
Removes an element in an array.
|
static <T> T[] |
remove(T[] array,
int index)
Removes an element at an index in an array.
|
static <T> T[] |
remove(T[] array,
T element)
Removes an element in an array.
|
static <T> boolean |
remove(T[] array,
T element,
int pointer)
Removes an element in an array.
|
static <T> T[] |
removeEquals(T[] array,
T element)
Removes an element in an array.
|
static <T> boolean |
removeEquals(T[] array,
T element,
int pointer)
Removes an element in an array.
|
static int[] |
removeRange(int[] array,
int offset,
int length)
Removes several elements in an array.
|
public static void checkBounds(byte[] bytes,
int offset,
int length)
bytes - the byte array.offset - the offset of the range.length - the length of the range.java.lang.NullPointerException - if bytes is null.java.lang.IndexOutOfBoundsException - if the range is not included in the array.public static void checkBounds(int arrayLength,
int offset,
int length)
arrayLength - the array length.offset - the offset of the range.length - the length of the range.java.lang.IndexOutOfBoundsException - if the range is not included in the array.public static void checkArrayBounds(int arrayLength,
int offset,
int length)
arrayLength - the array length.offset - the offset of the range.length - the length of the range.java.lang.ArrayIndexOutOfBoundsException - if the range is not included in the array.public static void checkStringBounds(int stringLength,
int offset,
int length)
stringLength - the string length.offset - the offset of the range.length - the length of the range.java.lang.StringIndexOutOfBoundsException - if the range is not included in the array.public static <T> T[] add(T[] array,
T element)
add(Object[], Object, int)).T - the type of array elements.array - the input array.element - the element to add.java.lang.NullPointerException - if the given array is null.public static <T> T[] add(T[] array,
T[] other)
add(Object[], Object, int)).T - the type of array elements.array - the input array.other - the array to append.java.lang.NullPointerException - if one of the given array is null.public static <T> T[] insert(T[] array,
int index,
T element)
insert(Object[], int, Object, int)).T - the type of array elements.array - the input array.index - the index in the array.element - the element to add.java.lang.NullPointerException - if the given array is null.java.lang.IndexOutOfBoundsException - if the given index is not included in the resulting array.public static <T> T[] remove(T[] array,
T element)
The comparison between the elements is done using == operator.
If the array does not contain the given element, the returned array is the given one, otherwise, it is always a different one. If the array contains several times the given element, only the last added is removed.
The returned array size fit the number of elements (in opposition with remove(Object[], Object, int)).
T - the type of array elements.array - the input array.element - the element to remove.java.lang.NullPointerException - if the given array is null.public static <T> T[] removeEquals(T[] array,
T element)
The comparison between the elements is done using Object.equals(Object).
If the array does not contain the given element, the returned array is the given one, otherwise, it is always a different one. If the array contains several times the given element, only the last added is removed.
The returned array size fit the number of elements (in opposition with remove(Object[], Object, int)).
T - the type of array elements.array - the input array.element - the element to remove.java.lang.NullPointerException - if the given array is null.public static <T> T[] remove(T[] array,
int index)
The returned array size fit the number of elements.
T - the type of array elements.array - the input array.index - the index of the element to remove.java.lang.NullPointerException - if the given array is null.java.lang.IndexOutOfBoundsException - if the given index is not included in the resulting array.public static <T> boolean contains(T[] array,
T element)
The comparison between the elements is done using == operator.
T - the type of array elements.array - the array.element - the element to search.true if the element exists in the array, false otherwise.java.lang.NullPointerException - if the given array is null.public static <T> boolean containsEquals(T[] array,
T element)
The comparison between the elements is done using Object.equals(Object).
T - the type of array elements.array - the array.element - the element to search.true if the element exists in the array, false otherwise.java.lang.NullPointerException - if the given array is null.public static <T> int getIndex(T[] array,
T element)
The comparison between the elements is done using == operator.
T - the type of array elements.array - the array.element - the element to search.-1 if not found.java.lang.NullPointerException - if the given array is null.public static <T> int getIndexEquals(T[] array,
T element)
The comparison between the elements is done using Object.equals(Object).
T - the type of array elements.array - the array.element - the element to search.-1 if not found.java.lang.NullPointerException - if the given array is null.public static <T> T[] copy(T[] array)
T - the type of array elements.array - the input array.java.lang.NullPointerException - if the given array is null.public static <T> T[] copy(java.lang.Object[] array,
java.lang.Class<T[]> type)
T - the type of array elements.array - the input array.type - the output array type.java.lang.NullPointerException - if the given type is null.java.lang.ArrayStoreException - if the given type is not an array type.java.lang.ArrayStoreException - if the given type mismatch the array elements type.public static int[] add(int[] array,
int element)
add(int[], int, int)).array - the input array.element - the element to add.java.lang.NullPointerException - if the given array is null.public static int[] remove(int[] array,
int element)
If the array does not contain the given element, the returned array is the given array, otherwise, it is always a different one. If the array contains several times the given element, only the last added is removed.
The returned array size fit the number of elements (in opposition with remove(int[], int, int)).
array - the input array.element - the element to remove.java.lang.NullPointerException - if the given array is null.public static int[] removeRange(int[] array,
int offset,
int length)
The returned array size fit the number of elements.
array - the input array.offset - the start index of the range to remove.length - the number of elements to remove.java.lang.NullPointerException - if the given array is null.public static int[] copy(int[] array)
array - the input array.java.lang.NullPointerException - if the given array is null.public static <T> T[] add(T[] array,
T element,
int pointer)
The array returned could be different from the given one, it grows to pointer*2 only if the given
array is not large enough to add the given element (pointer == array.length).
T - the type of array elements.array - the input array.element - the element to add.pointer - the index of the element to add.java.lang.ArrayIndexOutOfBoundsException - if the given pointer is greater than the array length.java.lang.NullPointerException - if the given array is null.public static <T> T[] insert(T[] array,
int index,
T element,
int pointer)
The array returned could be different from the given one, it grows to pointer*2 only if the given
array is not large enough to add the given element (pointer == array.length).
T - the type of array elements.array - the input array.index - the index in the array.element - the element to add.pointer - the index of the element to add.java.lang.NullPointerException - if the given array is null.java.lang.ArrayIndexOutOfBoundsException - if the given pointer is greater than the array length.java.lang.IndexOutOfBoundsException - if the given index is not included in the resulting array.public static <T> boolean remove(T[] array,
T element,
int pointer)
The comparison between the elements is done using == operator.
If the array does not contain the given element, the given array is not modified and the method returns
false, otherwise it returns true.
If the array contains several times the given element, only the last added is removed.
T - the type of array elements.array - the input array.element - the element to remove.pointer - the index of the last element of the array.true if the element has been removed, false otherwise.java.lang.ArrayIndexOutOfBoundsException - if the given pointer is greater than the array length.java.lang.NullPointerException - if the given array is null.public static <T> boolean removeEquals(T[] array,
T element,
int pointer)
The comparison between the elements is done using Object.equals(Object).
If the array does not contain the given element, the given array is not modified and the method returns
false, otherwise it returns true.
If the array contains several times the given element, only the last added is removed.
T - the type of array elements.array - the input array.element - the element to remove.pointer - the index of the last element of the array.true if the element has been removed, false otherwise.java.lang.ArrayIndexOutOfBoundsException - if the given pointer is greater than the array length.java.lang.NullPointerException - if the given array is null.public static <T> boolean contains(T[] array,
T element,
int pointer)
The comparison between the elements is done using == operator.
T - the type of array elements.array - the array.element - the element to search.pointer - the index of the last element of the array.true if the element exists in the array, false otherwise.java.lang.ArrayIndexOutOfBoundsException - if the given pointer is greater than the array length.java.lang.NullPointerException - if the given array is null.public static <T> boolean containsEquals(T[] array,
T element,
int pointer)
The comparison between the elements is done using Object.equals(Object).
T - the type of array elements.array - the array.element - the element to search.pointer - the index of the last element of the array.true if the element exists in the array, false otherwise.java.lang.ArrayIndexOutOfBoundsException - if the given pointer is greater than the array length.java.lang.NullPointerException - if the given array is null.public static <T> int getIndex(T[] array,
T element,
int pointer)
The comparison between the elements is done using == operator.
T - the type of array elements.array - the array.element - the element to search.pointer - the index of the last element of the array.-1 if not found.java.lang.ArrayIndexOutOfBoundsException - if the given pointer is greater than the array length.java.lang.NullPointerException - if the given array is null.public static <T> int getIndexEquals(T[] array,
T element,
int pointer)
The comparison between the elements is done using Object.equals(Object).
T - the type of array elements.array - the array.element - the element to search.pointer - the index of the last element of the array.-1 if not found.java.lang.ArrayIndexOutOfBoundsException - if the given pointer is greater than the array length.java.lang.NullPointerException - if the given array is null.public static <T> T[] copy(T[] array,
int pointer)
(pointer + 1)
elements of an array.T - the type of array elements.array - the input array.pointer - the index of the last element of the array.[0,pointer].java.lang.NullPointerException - if the given array is null.public static <T> T[] copy(java.lang.Object[] array,
int pointer,
java.lang.Class<T[]> type)
(pointer + 1) elements of an array.T - the type of array elements.array - the input arraypointer - the index of the last element of the arraytype - the output array type[0,pointer].java.lang.NullPointerException - if the given type is nulljava.lang.ArrayStoreException - if the given type is not an array typejava.lang.ArrayStoreException - if the given type mismatch the array elements typepublic static int[] add(int[] array,
int element,
int pointer)
The array returned could be different from the given one, it grows to pointer*2 only if the given
array is not large enough to add the given element (ptr == array.length).
array - the input array.element - the element to add.pointer - the index of the element to add.java.lang.ArrayIndexOutOfBoundsException - if the pointer is greater than the array length.java.lang.NullPointerException - if the given array is null.public static boolean remove(int[] array,
int element,
int pointer)
If the array does not contain the given element, the given array is not modified and the method returns
false, otherwise it returns true.
If the array contains several times the given element, only the last added is removed.
array - the input array.element - the element to remove.pointer - the index of the last element of the array.true if the element has been removed, false otherwise.java.lang.NullPointerException - if the given array is null.public static int[] copy(int[] array,
int pointer)
(ptr + 1) elements of an array.array - the input array.pointer - the index of the last element of the array.java.lang.NullPointerException - if the given array is null.public static <T> T[] createNewArray(T[] array,
int length)
T - the type of array elements.array - the array.length - the length of the array to create.public static <T> T[] createNewArray(java.lang.Class<T[]> type,
int length)
T - the type of array elements.type - the type of the array to create.length - the length of the array to create.