Java Reference
In-Depth Information
be copied. The second argument (
0
) is the index that specifies the
starting point
in the
range of elements to copy from the array. This value can be any valid array index. The
third argument (
intArrayCopy
) specifies the
destination array
that will store the copy. The
fourth argument (
0
) specifies the index in the destination array
where the first copied ele-
ment should be stored
. The last argument specifies the
number of elements to copy
from the
array in the first argument. In this case, we copy all the elements in the array.
Lines 30 and 35 call
static
method
equals
of class
Arrays
to determine whether all
the elements of two arrays are equivalent. If the arrays contain the same elements in the
same order, the method returns
true
; otherwise, it returns
false
.
Error-Prevention Tip 7.3
When comparing array contents, always use
Arrays.equals(array1,
array2)
, which
compares the two arrays' contents, rather than
array1.equals(array2)
, which compares
whether
array1
and
array2
refer to the same array object.
Lines 40 and 49 call
static
method
binarySearch
of class
Arrays
to perform a
binary search on
intArray
, using the second argument (
5
and
8763
, respectively) as the
key. If
value
is found,
binarySearch
returns the index of the element; otherwise,
bina-
rySearch
returns a negative value. The negative value returned is based on the search key's
insertion point
—the index where the key would be inserted in the array if we were per-
forming an insert operation. After
binarySearch
determines the insertion point, it
changes its sign to negative and subtracts
1
to obtain the return value. For example, in
Fig. 7.22, the insertion point for the value
8763
is the element with index
6
in the array.
Method
binarySearch
changes the insertion point to
-6
, subtracts
1
from it and returns
the value
-7
. Subtracting
1
from the insertion point guarantees that method
binarySearch
returns positive values (>= 0) if and only if the key is found. This return value is useful for
inserting elements in a sorted array. Chapter 19 discusses binary searching in detail.
Common Programming Error 7.6
Passing an unsorted array to
binarySearch
is a logic error—the value returned is unde-
fined.
Java SE 8—Class
Arrays
Method
parallelSort
The
Arrays
class now has several new “parallel” methods that take advantage of multi-core
hardware.
Arrays
method
parallelSort
can sort large arrays more efficiently on multi-
core systems. In Section 23.12, we create a very large array and use features of the
Java SE 8 Date/Time API to compare how long it takes to sort the array with methods
sort
and
parallelSort
.
The Java API provides several predefined data structures, called
collections
, used to store
groups of related objects in memory. These classes provide efficient methods that organize,
store and retrieve your data
without
requiring knowledge of how the data is being
stored
.
This reduces application-development time.
You've used arrays to store sequences of objects. Arrays do not automatically change
their size at execution time to accommodate additional elements. The collection class