Java Reference
In-Depth Information
At this point, myList is not empty, so myList.isEmpty() is false. Since the list contains three
entries, myList.getLength() is 3. Notice that adding entries to the end of a list does not change the
positions of entries already in the list. Figure 12-2 illustrates these add operations as well as the
operations that we describe next.
FIGURE 12-2
The effect of ADT list operations on an initially empty list
myList.add(c)
myList.add(a)
myList.add(b)
a
a
b
a
b
c
myList.add(2,d)
myList.add(1,e)
myList.remove(3)
a
d
b
c
e
a
d
b
c
e
a
b
c
12.4
Now suppose that we add entries at various positions within the list. For example,
myList.add(2, d)
places d second—that is, at position 2—within the list. Doing so, however, moves b to position 3
and c to position 4, so that the list now contains
a d b c
If we add e to the beginning of the list by writing
myList.add(1, e)
the current entries in the list move to the next higher position. The list then contains
e a d b c
Look at Figure 12-2 again to see the effect of these operations.
12.5
We can get the second entry in this list by writing
entry2 = myList.getEntry(2)
Remember that we are writing pseudocode here and ignoring details such as semicolons.
What happens when we remove an entry? For example,
myList.remove(3)
removes the third entry— d in the previous example—from the list. The list then contains
e a b c
Notice that entries after the one that was removed move to the next lower position within the
list. Figure 12-2 illustrates this change to the list.
Search WWH ::




Custom Search