Class ArrayTools
- java.lang.Object
-
- ej.basictool.ArrayTools
-
public final class ArrayTools extends Object
Defines a set of functions to manipulate arrays.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method 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 voidcheckArrayBounds(int arrayLength, int offset, int length)Checks the given range is included in the array (defined by its length).static voidcheckBounds(byte[] bytes, int offset, int length)Checks the given range is included in the given byte array.static voidcheckBounds(int arrayLength, int offset, int length)Checks the given range is included in the array (defined by its length).static voidcheckStringBounds(int stringLength, int offset, int length)Checks the given range is included in the array (defined by its length).static byte[]concat(byte[] array1, byte[] array2)Concatenates two arrays.static byte[]concatRanges(byte[] array1, int offset1, int length1, byte[] array2, int offset2, int length2)Concatenates two array ranges.static booleancontains(int[] array, int element)Gets whether an array contains an element or not.static booleancontains(int[] array, int element, int pointer)Gets whether an array contains an element or not.static <T> booleancontains(T[] array, T element)Gets whether an array contains an element or not.static <T> booleancontains(T[] array, T element, int pointer)Gets whether an array contains an element or not.static <T> booleancontainsEquals(T[] array, T element)Gets whether an array contains an element or not.static <T> booleancontainsEquals(T[] array, T element, int pointer)Gets whether an array contains an element or not.static <T> T[]createNewArray(T[] array, int length)Creates a new array with the same type as the given array.static booleanequalsRanges(byte[] array1, int offset1, byte[] array2, int offset2, int length)Returns whether two array ranges are equal.static intgetIndex(int[] array, int element)Gets the index of an element in an array.static <T> intgetIndex(T[] array, T element)Gets the index of an element in an array.static <T> intgetIndex(T[] array, T element, int pointer)Gets the index of an element in an array.static <T> intgetIndexEquals(T[] array, T element)Gets the index of an element in an array.static <T> intgetIndexEquals(T[] array, T element, int pointer)Gets the index of an element in an array.static int[]grow(int[] array, int index, int count)Grows an array by inserting a number of cells at an index.static <T> T[]grow(T[] array, int index, int count)Grows an array by inserting a number of cells at an index.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 booleanremove(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> booleanremove(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> booleanremoveEquals(T[] array, T element, int pointer)Removes an element in an array.static int[]shrink(int[] array, int index, int count)Shrinks an array by removing a number of cells at an index.static <T> T[]shrink(T[] array, int index, int count)Shrinks an array by removing a number of cells at an index.
-
-
-
Method Detail
-
checkBounds
public static void checkBounds(byte[] bytes, int offset, int length)Checks the given range is included in the given byte array.- Parameters:
bytes- the byte array.offset- the offset of the range.length- the length of the range.- Throws:
IndexOutOfBoundsException- if the range is not included in the array.
-
checkBounds
public static void checkBounds(int arrayLength, int offset, int length)Checks the given range is included in the array (defined by its length).- Parameters:
arrayLength- the array length.offset- the offset of the range.length- the length of the range.- Throws:
IndexOutOfBoundsException- if the range is not included in the array.
-
checkArrayBounds
public static void checkArrayBounds(int arrayLength, int offset, int length)Checks the given range is included in the array (defined by its length).- Parameters:
arrayLength- the array length.offset- the offset of the range.length- the length of the range.- Throws:
ArrayIndexOutOfBoundsException- if the range is not included in the array.
-
checkStringBounds
public static void checkStringBounds(int stringLength, int offset, int length)Checks the given range is included in the array (defined by its length).- Parameters:
stringLength- the string length.offset- the offset of the range.length- the length of the range.- Throws:
StringIndexOutOfBoundsException- if the range is not included in the array.
-
grow
public static int[] grow(int[] array, int index, int count)Grows an array by inserting a number of cells at an index. The new cells are initialized to0.- Parameters:
array- the input array.index- the index to insert the cells at in the array.count- the number of cells to insert.- Returns:
- the output array.
- Throws:
IllegalArgumentException- if the given count is negative.IndexOutOfBoundsException- if the given index is not included in the resulting array.
-
grow
public static <T> T[] grow(T[] array, int index, int count)Grows an array by inserting a number of cells at an index. The new cells are initialized tonull.- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.index- the index to insert the cells at in the array.count- the number of cells to insert.- Returns:
- the output array.
- Throws:
IllegalArgumentException- if the given count is negative.IndexOutOfBoundsException- if the given index is not included in the resulting array.
-
shrink
public static int[] shrink(int[] array, int index, int count)Shrinks an array by removing a number of cells at an index.- Parameters:
array- the input array.index- the index of the cells to remove in the array.count- the number of cells to remove.- Returns:
- the output array.
- Throws:
IllegalArgumentException- if the given count is negative or greater than array length.IndexOutOfBoundsException- if the given index is not included in the resulting array.
-
shrink
public static <T> T[] shrink(T[] array, int index, int count)Shrinks an array by removing a number of cells at an index.- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.index- the index of the cells to remove in the array.count- the number of cells to remove.- Returns:
- the output array.
- Throws:
IllegalArgumentException- if the given count is negative or greater than array length.IndexOutOfBoundsException- if the given index is not included in the resulting array.
-
add
public static <T> T[] add(T[] array, T element)Adds an element in an array. The array returned is always different from the given one, and its size fit the number of elements (in opposition withadd(Object[], Object, int)).- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.element- the element to add.- Returns:
- the output array.
-
add
public static <T> T[] add(T[] array, T[] other)Appends an array to another. The array returned is always different from the given one, and its size fit the number of elements (in opposition withadd(Object[], Object, int)).- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.other- the array to append.- Returns:
- the output array.
-
concat
public static byte[] concat(byte[] array1, byte[] array2)Concatenates two arrays.- Parameters:
array1- the first array.array2- the second array.- Returns:
- an array containing the concatenation of the arrays.
-
concatRanges
public static byte[] concatRanges(byte[] array1, int offset1, int length1, byte[] array2, int offset2, int length2)Concatenates two array ranges.- Parameters:
array1- the array containing the first range.offset1- the start index in the array of the first range.length1- the length of the first range.array2- the array containing the second range.offset2- the offset in the array of the second range.length2- the length of the second range.- Returns:
- an array containing the concatenation of the array ranges.
-
insert
public static <T> T[] insert(T[] array, int index, T element)Inserts an element at an index in an array. The array returned is always different from the given one, and its size fit the number of elements (in opposition withinsert(Object[], int, Object, int)).- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.index- the index in the array.element- the element to add.- Returns:
- the output array.
- Throws:
IndexOutOfBoundsException- if the given index is not included in the resulting array.
-
remove
public static <T> T[] remove(T[] array, T element)Removes an element in an array.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)).- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.element- the element to remove.- Returns:
- the output array.
-
removeEquals
public static <T> T[] removeEquals(T[] array, T element)Removes an element in an array.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)).- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.element- the element to remove.- Returns:
- the output array.
-
remove
public static <T> T[] remove(T[] array, int index)Removes an element at an index in an array.The returned array size fit the number of elements.
- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.index- the index of the element to remove.- Returns:
- the output array.
- Throws:
IndexOutOfBoundsException- if the given index is not included in the resulting array.
-
contains
public static <T> boolean contains(T[] array, T element)Gets whether an array contains an element or not.The comparison between the elements is done using
==operator.- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.element- the element to search.- Returns:
trueif the element exists in the array,falseotherwise.
-
containsEquals
public static <T> boolean containsEquals(T[] array, T element)Gets whether an array contains an element or not.The comparison between the elements is done using
Object.equals(Object).- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.element- the element to search.- Returns:
trueif the element exists in the array,falseotherwise.
-
getIndex
public static int getIndex(int[] array, int element)Gets the index of an element in an array. If the array contains several times the given element, the index of the last added is returned.- Parameters:
array- the array.element- the element to search.- Returns:
- the index of the element in the array or
-1if not found.
-
getIndex
public static <T> int getIndex(T[] array, T element)Gets the index of an element in an array. If the array contains several times the given element, the index of the last added is returned.The comparison between the elements is done using
==operator.- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.element- the element to search.- Returns:
- the index of the element in the array or
-1if not found.
-
getIndexEquals
public static <T> int getIndexEquals(T[] array, T element)Gets the index of an element in an array. If the array contains several times the given element, the index of the last added is returned.The comparison between the elements is done using
Object.equals(Object).- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.element- the element to search.- Returns:
- the index of the element in the array or
-1if not found.
-
add
public static int[] add(int[] array, int element)Adds an element in an array. The array returned is always different from the given one, and its size fit the number of elements (in opposition withadd(int[], int, int)).- Parameters:
array- the input array.element- the element to add.- Returns:
- the output array.
-
remove
public static int[] remove(int[] array, int element)Removes an element in an array.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)).- Parameters:
array- the input array.element- the element to remove.- Returns:
- the output array.
-
contains
public static boolean contains(int[] array, int element)Gets whether an array contains an element or not.- Parameters:
array- the array.element- the element to search.- Returns:
trueif the element exists in the array,falseotherwise.
-
equalsRanges
public static boolean equalsRanges(byte[] array1, int offset1, byte[] array2, int offset2, int length)Returns whether two array ranges are equal.- Parameters:
array1- the array containing the first range.offset1- the start index in the array of the first range.array2- the array containing the second range.offset2- the offset in the array of the second range.length- the length of the ranges.- Returns:
trueif the array ranges are equal,falseotherwise.
-
add
public static <T> T[] add(T[] array, T element, int pointer)Adds an element in an array.The array returned could be different from the given one, it grows to
pointer*2only if the given array is not large enough to add the given element (pointer == array.length).- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.element- the element to add.pointer- the index of the element to add.- Returns:
- the output array.
- Throws:
ArrayIndexOutOfBoundsException- if the given pointer is greater than the array length.
-
insert
public static <T> T[] insert(T[] array, int index, T element, int pointer)Inserts an element at an index in an array.The array returned could be different from the given one, it grows to
pointer*2only if the given array is not large enough to add the given element (pointer == array.length).- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.index- the index in the array.element- the element to add.pointer- the index of the element to add.- Returns:
- the output array.
- Throws:
ArrayIndexOutOfBoundsException- if the given pointer is greater than the array length.IndexOutOfBoundsException- if the given index is not included in the resulting array.
-
remove
public static <T> boolean remove(T[] array, T element, int pointer)Removes an element in an array.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 returnstrue.If the array contains several times the given element, only the last added is removed.
- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.element- the element to remove.pointer- the index of the last element of the array.- Returns:
trueif the element has been removed,falseotherwise.- Throws:
ArrayIndexOutOfBoundsException- if the given pointer is greater than the array length.
-
removeEquals
public static <T> boolean removeEquals(T[] array, T element, int pointer)Removes an element in an array.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 returnstrue.If the array contains several times the given element, only the last added is removed.
- Type Parameters:
T- the type of array elements.- Parameters:
array- the input array.element- the element to remove.pointer- the index of the last element of the array.- Returns:
trueif the element has been removed,falseotherwise.- Throws:
ArrayIndexOutOfBoundsException- if the given pointer is greater than the array length.
-
contains
public static <T> boolean contains(T[] array, T element, int pointer)Gets whether an array contains an element or not.The comparison between the elements is done using
==operator.- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.element- the element to search.pointer- the index of the last element of the array.- Returns:
trueif the element exists in the array,falseotherwise.- Throws:
ArrayIndexOutOfBoundsException- if the given pointer is greater than the array length.
-
containsEquals
public static <T> boolean containsEquals(T[] array, T element, int pointer)Gets whether an array contains an element or not.The comparison between the elements is done using
Object.equals(Object).- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.element- the element to search.pointer- the index of the last element of the array.- Returns:
trueif the element exists in the array,falseotherwise.- Throws:
ArrayIndexOutOfBoundsException- if the given pointer is greater than the array length.
-
getIndex
public static <T> int getIndex(T[] array, T element, int pointer)Gets the index of an element in an array. If the array contains several times the given element, the index of the last added is returned.The comparison between the elements is done using
==operator.- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.element- the element to search.pointer- the index of the last element of the array.- Returns:
- the index of the element in the array or
-1if not found. - Throws:
ArrayIndexOutOfBoundsException- if the given pointer is greater than the array length.
-
getIndexEquals
public static <T> int getIndexEquals(T[] array, T element, int pointer)Gets the index of an element in an array. If the array contains several times the given element, the index of the last added is returned.The comparison between the elements is done using
Object.equals(Object).- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.element- the element to search.pointer- the index of the last element of the array.- Returns:
- the index of the element in the array or
-1if not found. - Throws:
ArrayIndexOutOfBoundsException- if the given pointer is greater than the array length.
-
add
public static int[] add(int[] array, int element, int pointer)Adds an element in an array.The array returned could be different from the given one, it grows to
pointer*2only if the given array is not large enough to add the given element (ptr == array.length).- Parameters:
array- the input array.element- the element to add.pointer- the index of the element to add.- Returns:
- the output array.
- Throws:
ArrayIndexOutOfBoundsException- if the pointer is greater than the array length.
-
remove
public static boolean remove(int[] array, int element, int pointer)Removes an element in an array.If the array does not contain the given element, the given array is not modified and the method returns
false, otherwise it returnstrue.If the array contains several times the given element, only the last added is removed.
- Parameters:
array- the input array.element- the element to remove.pointer- the index of the last element of the array.- Returns:
trueif the element has been removed,falseotherwise.
-
contains
public static boolean contains(int[] array, int element, int pointer)Gets whether an array contains an element or not.- Parameters:
array- the array.element- the element to search.pointer- the index of the last element of the array.- Returns:
trueif the element exists in the array,falseotherwise.
-
createNewArray
public static <T> T[] createNewArray(T[] array, int length)Creates a new array with the same type as the given array.- Type Parameters:
T- the type of array elements.- Parameters:
array- the array.length- the length of the array to create.- Returns:
- the created array as an object
-
-