Java Reference
In-Depth Information
20.6
(Overloading a Generic Method with a Nongeneric Method))
Overload generic method
printArray
of Fig. 20.3 with a nongeneric version that specifically prints an array of
String
s in neat,
tabular format, as shown in the sample output that follows:
Array stringArray contains:
one two three four
five six seven eight
20.7
(Generic
isEqualTo
Method)
Write a simple generic version of method
isEqualTo
that
compares its two arguments with the
equals
method and returns
true
if they're equal and
false
otherwise. Use this generic method in a program that calls
isEqualTo
with a variety of built-in types,
such as
Object
or
Integer
. What result do you get when you attempt to run this program?
20.8
(Generic Class
Pair
)
Write a generic class
Pair
which has two type parameters—
F
and
S
—
each representing the type of the first and second element of the pair, respectively. Add
get
and
set
methods for the first and second elements of the pair. [
Hint:
The class header should be
public
class
Pair
<
F
,
S
>.]
20.9
(Overloading Generic Methods)
How can generic methods be overloaded?
20.10
(Overload Resolution)
The compiler performs a matching process to determine which
method to call when a method is invoked. Under what circumstances does an attempt to make a
match result in a compile-time error?
20.11
(What Does this Statement Do?)
Explain why a Java program might use the statement
ArrayList<Employee> workerList =
new
ArrayList<>();