Java Reference
In-Depth Information
Question 5
Suppose that the return type of
getEntry
was
Object
instead of a generic type.
Would this change affect how you use the method? In particular, would the statement in the
previous example that retrieved the second name in
nameList
be correct? Why?
12.11
Example.
Let's talk a bit more about the previous example. The variable
secondName
is a reference
to the second object in the list. Using this reference, we can modify the object. For example, we
could change its last name by writing
secondName.setLast("Doe");
If the class
Name
did not have set methods like
setLast
, we would be unable to modify the
objects in this list. For instance, if we had a list of strings, we would not be able to alter one of the
strings in this way. The class
String
has no set methods, so once we create a
String
object, we
cannot alter it. We could, however, replace an entire object in the list—regardless of its type—by
using the ADT list operation
replace
.
Recall that Chapter 10 mentioned mutable and immutable objects. Since the class
Name
has set
methods, its objects are mutable. The class
String
, on the other hand, does not define set methods,
so its objects are immutable.
The standard package
java.util
contains an interface
List
for an ADT list that is similar to the list that
our interface describes. One difference between a list in the Java Class Library and our ADT list is the num-
bering of a list's entries. A list in the Java Class Library uses the same numbering scheme as a Java array:
The first entry is at position, or index, 0. In contrast, we begin our list at position 1. The interface
List
also
declares more methods than our interface does. You'll see a few of those additional methods in Chapter 15.
The following method headers from the interface
List
are for a selection of methods that are
similar to the ones you have seen in this chapter. We have highlighted where they differ from our
methods. Once again,
T
is the generic type of the entries in the list.
public
boolean
add(T newEntry)
public
void
add(
int
index, T newEntry)
public
T remove(
int
index)
public
void
clear()
public
T set(
int
index, T anEntry)
// like replace
public
T get(
int
index)
// like getEntry
public
boolean
contains(Object anEntry)
public
int
size()
// like getLength
public
boolean
isEmpty()
The first
add
method, which adds an entry to the end of a list, returns a boolean value, whereas our analo-
gous method is a void method. The second
add
method is a void method. It throws an exception if
index
is
out of range, instead of returning the boolean value false, as our
add
method does. The methods
remove
and
get
also throw an exception if
index
is out of range. Our analogous methods return
null
instead. The
method
set
is like our
replace
method, but it returns a reference to the entry that was replaced in the list
instead of returning a boolean value. It also throws an exception if
index
is out of range. The data type of
contains
' parameter is
Object
instead of a generic type. In practice, this difference has little consequence.
Lastly, the method
get
is like our
getEntry
, and
size
is like our
getLength
.
You can learn more about the interface
List
in the online documentation for the Java Class Library.
The Java Class Library contains an implementation of the ADT list that uses a resizable array. This
class, called
ArrayList
, implements the interface
java.util.List
, that we just discussed. The
class also is in the package
java.util
.