public class Arrays
extends java.lang.Object
Arrays
contains static methods which operate on arrays.Modifier and Type | Method and Description |
---|---|
static <T> int |
binarySearch(T[] array,
int startIndex,
int endIndex,
T object,
Comparator<T> comparator)
Performs a binary search for the specified element in a part of the specified sorted array using the Comparator
to compare elements.
|
static <T> int |
binarySearch(T[] array,
T object,
Comparator<T> comparator)
Performs a binary search for the specified element in the specified sorted array using the Comparator to compare
elements.
|
static <T> boolean |
equals(T[] array1,
T[] array2)
Compares the two arrays.
|
static <T> void |
fill(T[] array,
int start,
int end,
T value)
Fills the specified range in the array with the specified element.
|
static <T> void |
fill(T[] array,
T value)
Fills the specified array with the specified element.
|
static <T> int |
hashCode(T[] array)
Returns a hash code based on the contents of the given array.
|
static <T> void |
sort(T[] array,
Comparator<T> comparator)
Sorts the specified array using the specified
Comparator . |
static <T> void |
sort(T[] array,
int start,
int end,
Comparator<T> comparator)
Sorts the specified array range, from start (inclusive) to end (exclusive), using the specified
Comparator . |
static <T> java.lang.String |
toString(T[] array)
Creates a
String representation of the array passed. |
public static <T> int binarySearch(T[] array, T object, Comparator<T> comparator)
T
- type of objectarray
- the sorted array to searchobject
- the element to findcomparator
- the Comparatorpublic static <T> int binarySearch(T[] array, int startIndex, int endIndex, T object, Comparator<T> comparator)
T
- type of objectarray
- the sorted array to searchstartIndex
- the inclusive start indexendIndex
- the exclusive end indexobject
- the value element to findcomparator
- the ComparatorProgrammerError
- - if startIndex is bigger than endIndexProgrammerError
- - if startIndex is smaller than zero or or endIndex is bigger than length of arraypublic static <T> void fill(T[] array, T value)
array
- the array to fill.value
- the element to fill withpublic static <T> void fill(T[] array, int start, int end, T value)
array
- the array to fill.start
- the first index to fill.end
- the last + 1 index to fill.value
- the element.ProgrammerError
- if start > end
.ProgrammerError
- if start < 0
or end > array.length
.public static <T> int hashCode(T[] array)
a
and b
, if Arrays.equals(a, b)
returns true
, it means that
the return value of Arrays.hashCode(a)
equals Arrays.hashCode(b)
.
The value returned by this method is the same value as the method Arrays.asList(array).hashCode(). If the array
is null
, the return value is 0.array
- the array whose hash code to compute.array
.public static <T> boolean equals(T[] array1, T[] array2)
array1
- the first array.array2
- the second array.true
if both arrays are null
or if the arrays have the same length and the elements at
each index in the two arrays are equal according to equals()
, false
otherwise.public static <T> void sort(T[] array, int start, int end, Comparator<T> comparator)
Comparator
.array
- the array to be sorted.start
- the start index to sort.end
- the last + 1 index to sort.comparator
- the Comparator
.ProgrammerError
- if start > end
.ProgrammerError
- if start < 0
or end > array.length
.public static <T> void sort(T[] array, Comparator<T> comparator)
Comparator
.array
- the array to be sortedcomparator
- the Comparator
public static <T> java.lang.String toString(T[] array)
String
representation of the array passed. The result is surrounded by brackets ("[]"
), each element is converted to a String
via the String.valueOf(Object)
and
separated by ", "
. If the array is null
, then "null"
is returned.array
- the array to convert.String
representation of array
.