Java Reference
In-Depth Information
Our array list of integers can be converted into a generic class
that can store a list of any type of objects. The code is similar,
but we must make a few changes—for example, when we
construct arrays of type E[] or compare objects for equality.
An inner class is declared inside the braces of another
(outer) class and has access to the state of an object
of that outer class. Our final list iterator is an inner class.
Self-Check Problems
Section 15.1: Simple ArrayIntList
1. What is the difference between an array list's size and its capacity? What is the relationship between the two values?
(Is one always larger or smaller than the other, for instance?)
2. What fields must be included in the ArrayIntList class, and why is each field important? Would the class still
work correctly if we removed any of these fields?
3. How would the output of the Client1 program shown in this section change if each field from ArrayIntList
were declared static ?
4. In this version of the list class, what happens if the client adds too many values to fit in the array?
5. Why does the list class use a toString method rather than a print method?
6. We wrote the class to have public methods called size (to read the number of elements of the list) and get (to
access the element value at a specific index). Why is this approach better than declaring the fields (such as size )
public ?
7. An element can be inserted at the beginning, middle, or end of an array list. Which of the three insertion points
is the most computationally expensive, and why? Which is the most expensive location to remove an element
from the list?
8. Write methods called min and max that return the smallest and largest values in the list respectively. For example, if
a variable called list stores [11, -7, 3, 42, 0, 14] , the call of list.min() should return -7 and the call of
list.max() should return 42 . If the list is empty, the methods should throw an IllegalStateException .
Section 15.2:A More Complete ArrayIntList
9. Describe the overall preconditions placed on the list class in this section. What assumptions do we make about how
clients will use the list?
10. What is the purpose of the checkIndex method? Where is it called in the list class? Describe a way that the client
can utilize an ArrayIntList that will be caught by checkIndex .
11. What is the purpose of the checkCapacity method? Where is it called in the list class? Describe a way that the
client can utilize an ArrayIntList that will be caught by checkCapacity .
12. Once we check thoroughly for preconditions in the code, what data invariants can we now assume about the list?
13. Why do we bother to add the contains , isEmpty , and remove methods to the list class, when the client can
already perform this same functionality with the indexOf , size , and remove methods respectively?
 
 
Search WWH ::




Custom Search