public final class ArrayUtil
extends java.lang.Object
Modifier | Constructor and Description |
---|---|
private |
ArrayUtil()
Forbid instances by this private constructor.
|
Modifier and Type | Method and Description |
---|---|
static int[] |
add(int[] array,
int toAdd)
Adds the given element to the existing array.
|
static <T> T[] |
add(T[] array,
T toAdd)
Adds the given element to the existing array.
|
static <T> T[] |
addAll(T[] array,
T[] toAdd)
Adds the given elements to the existing array.
|
static <T> T[] |
addAll(T[] array,
T[] toAdd,
java.lang.Class<?> newArrayType)
Adds the given elements to the existing array.
|
static <T> boolean |
contains(T[] array,
T toSearch)
Checks if the given array contains the element to search.
|
static <T> boolean |
contains(T[] array,
T toSearch,
java.util.Comparator<T> comparator)
Checks if the given array contains the element to search.
|
static <T> T[][] |
generatePermutations(T[] array)
Computes all permutations of the given array using the
'Heap's algorithm'.
|
private static <T> int |
generatePermutations(T[] array,
int n,
T[][] permutations,
int permutationsIndex)
Recursive implementation of the 'Heap's algorithm':
procedure generate(n : integer, A : array of any):
if n = 1 then
output(A)
else
for i := 0; i < n; i += 1 do
generate(n - 1, A)
if n is even then
swap(A[i], A[n-1])
else
swap(A[0], A[n-1])
end if
end for
end if
|
static <T> T |
getFirst(T[] array)
Returns the first element from the given array.
|
static <T> T |
getFollowing(T[] array,
T toSearch)
Returns the following element if available from the array.
|
static <T> T |
getFollowing(T[] array,
T toSearch,
java.util.Comparator<T> comparator)
Returns the following element if available from the array.
|
static <T> T |
getLast(T[] array)
Returns the last element from the given array.
|
static <T> T |
getPrevious(T[] array,
T toSearch)
Returns the previous element if available from the array.
|
static <T> T |
getPrevious(T[] array,
T toSearch,
java.util.Comparator<T> comparator)
Returns the previous element if available from the array.
|
static <T> int |
indexOf(T[] array,
T toSearch)
Returns the first index in the given array that contains the
element to search.
|
static <T> int |
indexOf(T[] array,
T toSearch,
java.util.Comparator<T> comparator)
Returns the first index in the given array that contains the
element to search.
|
static <T> T[] |
insert(T[] array,
T toInsert,
int index)
Inserts the given element at the given index to the existing array.
|
static <T> boolean |
isEmpty(T[] array)
Checks if the given array is empty.
|
static <T> boolean |
isFirst(T[] array,
T toSearch)
Checks if the given element is the first element in the array.
|
static <T> boolean |
isFirst(T[] array,
T toSearch,
java.util.Comparator<T> comparator)
Checks if the given element is the first element in the array.
|
static <T> boolean |
isLast(T[] array,
T toSearch)
Checks if the given element is the last element in the array.
|
static <T> boolean |
isLast(T[] array,
T toSearch,
java.util.Comparator<T> comparator)
Checks if the given element is the last element in the array.
|
static <T> T[] |
remove(T[] array,
T toRemove)
Removes all occurrences from toRemove in the array.
|
static <T> T[] |
remove(T[] array,
T toRemove,
java.util.Comparator<T> comparator)
Removes all occurrences from toRemove in the array.
|
static <T> T |
search(T[] array,
IFilter<T> filter)
Searches an element in the given
Iterable instance. |
static java.lang.String |
toString(int[] array)
Converts the given array into a
String . |
static java.lang.String |
toString(int[] array,
java.lang.String separator)
Converts the given array into a
String . |
static <T> java.lang.String |
toString(T... array)
Converts the given array into a
String . |
static <T> java.lang.String |
toString(T[] array,
java.lang.String separator)
Converts the given array into a
String . |
public static <T> T search(T[] array, IFilter<T> filter)
Iterable
instance.array
- The instance to search in.filter
- The filter to select an element.null
if no element was found.public static <T> T[] addAll(T[] array, T[] toAdd)
Adds the given elements to the existing array. The result is a new array that contains the other elements in the end.
Attention: It is not possible to use this method with
two null
parameters. In this case is an IllegalArgumentException
thrown.
array
- The array to add to.toAdd
- The elements to add.java.lang.IllegalArgumentException
- Both parameters are null
.public static <T> T[] addAll(T[] array, T[] toAdd, java.lang.Class<?> newArrayType)
Adds the given elements to the existing array. The result is a new array that contains the other elements in the end.
Attention: It is not possible to use this method with
two null
parameters. In this case is an IllegalArgumentException
thrown.
array
- The array to add to.toAdd
- The elements to add.newArrayType
- The type of the new array.java.lang.IllegalArgumentException
- Both parameters are null
.public static <T> T[] add(T[] array, T toAdd)
Adds the given element to the existing array. The result is a new array that contains one more element.
Attention: It is not possible to use this method with
two null
parameters. In this case is an IllegalArgumentException
thrown.
array
- The array to extend.toAdd
- The element to add.java.lang.IllegalArgumentException
- Both parameters are null
.public static int[] add(int[] array, int toAdd)
Adds the given element to the existing array. The result is a new array that contains one more element.
array
- The array to extend.toAdd
- The element to add.public static <T> T[] insert(T[] array, T toInsert, int index)
Inserts the given element at the given index to the existing array. The result is a new array that contains one more element.
array
- The array to extend.toInsert
- The element to insert.index
- The index to insert the element at.public static <T> boolean contains(T[] array, T toSearch)
T
- The type of the array.array
- The array.toSearch
- The element to search.true
if the array contains the element or false
if not or if the array is null
.public static <T> boolean contains(T[] array, T toSearch, java.util.Comparator<T> comparator)
0
.T
- The type of the array.array
- The array.toSearch
- The element to search.comparator
- the Comparator
to use.true
if the array contains the element or false
if not or if the array is null
.java.lang.IllegalArgumentException
- If the comparator is null
.public static <T> int indexOf(T[] array, T toSearch)
array
- The array to search in.toSearch
- The element to search.-1
if the elment is not containd in the array.public static <T> int indexOf(T[] array, T toSearch, java.util.Comparator<T> comparator)
0
.array
- The array to search in.toSearch
- The element to search.comparator
- the Comparator
to use.-1
if the elment is not containd in the array.java.lang.IllegalArgumentException
- If the comparator is null
.public static <T> T[] remove(T[] array, T toRemove)
ObjectUtil.equals(Object, Object)
.array
- The array to remove from.toRemove
- The element to remove.null
if the given array was null
.public static <T> T[] remove(T[] array, T toRemove, java.util.Comparator<T> comparator)
0
.array
- The array to remove from.toRemove
- The element to remove.comparator
- The Comparator
to use.null
if the given array was null
.java.lang.IllegalArgumentException
- If the comparator is null
.public static <T> java.lang.String toString(T... array)
String
.
For each element is Object.toString()
used to convert it
into a String
.array
- The array to convert.String
.public static <T> java.lang.String toString(T[] array, java.lang.String separator)
String
.
For each element is Object.toString()
used to convert it
into a String
.array
- The array to convert.separator
- The separator between to array elements.String
.public static java.lang.String toString(int[] array)
String
.array
- The array to convert.String
.public static java.lang.String toString(int[] array, java.lang.String separator)
String
.array
- The array to convert.separator
- The separator between to array elements.String
.public static <T> boolean isEmpty(T[] array)
array
- The array to check.true
array is empty or null
, false
array is not empty.public static <T> T getPrevious(T[] array, T toSearch)
array
- The array to search in.toSearch
- The element for that the previous one is needed.null
if no element was found.public static <T> T getPrevious(T[] array, T toSearch, java.util.Comparator<T> comparator) throws java.lang.IllegalArgumentException
0
.array
- The array to search in.toSearch
- The element for that the previous one is needed.comparator
- the Comparator
to use.null
if no element was found.java.lang.IllegalArgumentException
- If the comparator is null
.public static <T> T getFollowing(T[] array, T toSearch)
array
- The array to search in.toSearch
- The element for that the following one is needed.null
if no element was found.public static <T> T getFollowing(T[] array, T toSearch, java.util.Comparator<T> comparator) throws java.lang.IllegalArgumentException
0
.array
- The array to search in.toSearch
- The element for that the following one is needed.comparator
- the Comparator
to use.null
if no element was found.java.lang.IllegalArgumentException
- If the comparator is null
.public static <T> boolean isLast(T[] array, T toSearch)
T
- The type of the array.array
- The array.toSearch
- The element to search.true
is last element, false
is not last element or even not contained in the array.public static <T> boolean isLast(T[] array, T toSearch, java.util.Comparator<T> comparator)
0
.T
- The type of the array.array
- The array.toSearch
- The element to search.comparator
- the Comparator
to use.true
is last element, false
is not last element or even not contained in the array.java.lang.IllegalArgumentException
- If the comparator is null
.public static <T> boolean isFirst(T[] array, T toSearch)
T
- The type of the array.array
- The array.toSearch
- The element to search.true
is first element, false
is not first element or even not contained in the array.public static <T> boolean isFirst(T[] array, T toSearch, java.util.Comparator<T> comparator)
0
.T
- The type of the array.array
- The array.toSearch
- The element to search.comparator
- the Comparator
to use.true
is first element, false
is not first element or even not contained in the array.java.lang.IllegalArgumentException
- If the comparator is null
.public static <T> T getFirst(T[] array)
array
- The array to get first element from.null
if no element is available.public static <T> T getLast(T[] array)
array
- The array to get last element from.null
if no element is available.public static <T> T[][] generatePermutations(T[] array)
array
- The array to generate its permutations.null
if the given array is null
.https://en.wikipedia.org/wiki/Heap's_algorithm
private static <T> int generatePermutations(T[] array, int n, T[][] permutations, int permutationsIndex)
procedure generate(n : integer, A : array of any):
if n = 1 then
output(A)
else
for i := 0; i < n; i += 1 do
generate(n - 1, A)
if n is even then
swap(A[i], A[n-1])
else
swap(A[0], A[n-1])
end if
end for
end if
array
- The array to generate its permutations.n
- The recursive termination criterion.permutations
- The result List
to fill.https://en.wikipedia.org/wiki/Heap's_algorithm