Java Reference
In-Depth Information
public double average() {
if (isEmpty()) {
return 0.0;
} else {
return (double) sum() / size;
}
}
20. Java does not allow the construction of arrays of generic types. To work around
this, we instead create an array of Object[] and cast it to type E[] .
21. We must modify indexOf to compare objects using equals rather than
== because == compares only references and not the state of the objects.
22. An annotation is a special directive to the compiler with additional information
about a class, method, or other structure. Annotations help us when we are writing
our generic list because we can instruct the compiler not to warn us about poten-
tially unsafe casting operations.
23. It is important to set the removed/cleared elements to null so that Java's garbage
collector can potentially reclaim their memory.
24. When the iterator is an inner class, it can directly access the fields of the enclosing
list object. This makes it easier for the iterator to do its work without keeping track
of as much state.
Chapter 16
1. The difference between a linked list and an array list is that while an array list
stores all of its elements in a single large array, a linked list stores each element
inside its own container object called a node. The nodes are connected (linked) to
each other by references. The two kinds of lists are similar in that they both imple-
ment the same external operations to clients, such as methods for adding, remov-
ing, accessing, and locating elements.
2. A node is a small object that stores a single element of a linked list. The list object
stores reference(s) to a small number of nodes, perhaps only the front of the list. The
front contains a chain of references that connect to the other elements of the list.
3. The next field of the last node of a list, as well as any unspecified next field,
stores null .
4. When the client tries to go past the end of a linked list, the program throws a null
pointer exception.
5.
list
1
3
6. list
1
3
2
 
 
Search WWH ::




Custom Search