Java Reference
In-Depth Information
Section 7.15 Class
Arrays
• Class
Arrays
(p. 285) provides
static
methods that peform common array manipulations, in-
cluding
sort
to sort an array,
binarySearch
to search a sorted array,
equals
to compare arrays
and
fill
to place items in an array.
• Class
System
's
arraycopy
method (p. 285) enables you to copy the elements of one array into
another.
Section 7.16 Introduction to Collections and Class
ArrayList
• The Java API's collection classes provide efficient methods that organize, store and retrieve data
without requiring knowledge of how the data is being stored.
•An
ArrayList<T>
(p. 288) is similar to an array but can be dynamically resized.
•The
add
method (p. 290) with one argument appends an element to the end of an
ArrayList
.
•The
add
method with two arguments inserts a new element at a specified position in an
ArrayList
.
•The
size
method (p. 290) returns the number of elements currently in an
ArrayList
.
•The
remove
method with a reference to an object as an argument removes the first element that
matches the argument's value.
•The
remove
method with an integer argument removes the element at the specified index, and
all elements above that index are shifted down by one.
•The
contains
method returns
true
if the element is found in the
ArrayList
, and
false
otherwise.
Self-Review Exercises
7.1
Fill in the blank(s) in each of the following statements:
a)
Lists and tables of values can be stored in
and
.
b)
An array is a group of
(called elements or components) containing values that
all have the same
.
c)
The
allows you to iterate through an array's elements without using a counter.
d)
The number used to refer to a particular array element is called the element's
.
e)
An array that uses two indices is referred to as a(n)
array.
f)
Use the enhanced
for
statement
to walk through
double
array
numbers
.
g)
Command-line arguments are stored in
.
h)
Use the expression to receive the total number of arguments in a command
line. Assume that command-line arguments are stored in
String[] args
.
i)
Given the command
java
MyClass
test
, the first command-line argument is
.
j)
A(n) in the parameter list of a method indicates that the method can receive
a variable number of arguments.
7.2
Determine whether each of the following is
true
or
false
. If
false
, explain why.
a)
An array can store many different types of values.
b)
An array index should normally be of type
float
.
c)
An individual array element that's passed to a method and modified in that method will
contain the modified value when the called method completes execution.
d)
Command-line arguments are separated by commas.
7.3
Perform the following tasks for an array called
fractions
:
a)
Declare a constant
ARRAY_SIZE
that's initialized to 10.
b)
Declare an array with
ARRAY_SIZE
elements of type
double
, and initialize the elements to
0
.
c)
Refer to array element 4.
d)
Assign the value
1.667
to array element 9.
e)
Assign the value
3.333
to array element 6.