Java Reference
In-Depth Information
21.12. The
Arrays
Utility Class
The class
Arrays
provides useful static methods for dealing with arrays.
Most of these methods have a full complement of overloads: one for ar-
rays of each primitive type (except
boolean
for searching and sorting) and
one for
Object
arrays. There are also two variants of some methods: one
acting on the whole array and one acting on the subarray specified by
two supplied indices. The methods are
•
sort
Sorts an array into ascending order. The exact algorithm is
not specified other than it must be stable for objects (that is, equal
objects don't get reordered because of the sort). A good imple-
mentation would use a technique that is not worse than
O(nlogn)
•
binarySearch
Searches a sorted array for a given key. Returns the
key's index, or a negative value encoding a safe insertion point
(as for the method
Collections.binarySearch
described previously).
There are no subarray versions of these methods.
•
fill
Fills in the array with a specified value.
•
equals
and
deepEquals
Return
true
if the two arrays they are passed
are the same object, are both
null
, or have the same size and
equivalent contents. There are no subarray versions. The
equals
method for
Object[]
uses
Object.equals
on each non-
null
element
of the array;
null
elements in the first array must be matched
by
null
elements of the second. This does not treat nested arrays
specially, so it cannot generally be used to compare arrays of ar-
rays. The
deepEquals
method checks for equivalance of two
Object[]
recursively taking into account the equivalence of nested arrays.
•
hashCode
and
deepHashCode
Return a hash code based on the con-
tents of the given array. There are no subarray versions. The
deep-
HashCode
method computes a hash code for an
Object[]
recursively
taking into account the contents of nested arrays.