Java Reference
In-Depth Information
return (double) total / count;
}
}
29. The main advantage of the IntList interface is that client code can take advan-
tage of polymorphism. A client program can deal with an IntList reference and
the actual object at runtime can be an instance of either kind of list.
30. public void firstLast(IntList list) {
if (!list.isEmpty()) {
int element = list.get(0);
list.remove(0);
list.add(element);
}
}
31. When you are changing the linked list to store elements of type E , the list class, its
nested classes, and several methods must be changed to use the new generic type.
We must change any comparisons between objects to use equals instead of == . In
our code, we also use dummy header nodes and add a back reference to increase
the efficiency when values are added to the end of the list.
32. Iterators are important with linked lists because it is inefficient to traverse a linked
list by calling the get method once for each index. Such an algorithm must repeat-
edly traverse the entire list to each index passed. The iterator instead remembers its
position between calls to next .
33. The linked list iterator keeps a reference to its current node and a boolean for
whether it is safe to remove an element. The iterator knows that there are more
elements by looking at the next reference of its current node.
Chapter 17
1. A tree has only one root. A tree could have more leaves than branches (for exam-
ple, a perfect tree of height 3), or it could have more branches than leaves (for
example, a tree whose root has two child nodes, each of which has one child, each
of which has one child).
2. Here is a tree with twice as many leaves as branches:
1
2
3
 
Search WWH ::




Custom Search