Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^548) | Array-Based Lists
publicSortedList()
{
super();
}
publicSortedList(int maxItems)
{
super(maxItems);
}
public voidinsert(String item)
// If the list is not full, puts item in its proper place in
// the list; otherwise list is unchanged
// Assumption: item is not already in the list
{
if (!isFull())
{
int index = numItems – 1; // Loop control variable
while(index >= 0 && (item.compareTo(listItems[index]) < 0))
{ // Find insertion point
listItems[index+1] = listItems[index];
index--;
}
listItems[index+1] = item; // Insert item
numItems++; // Increment number of items
}
}
}


Test Plan


We can use the same test plan for the sorted list that we used for the unsorted version. The
only difference is that in the expected output, the list items should appear in sorted order.
Alternatively, we could use the same input file that was used to test ListWithSortto test
SortedList.

11.5 The List Class Hierarchy and Abstract Classes


We have created a hierarchy with the class Listat the top and two derived classes. We can
visualize this hierarchy as follows:

List

ListWithSort SortedList
Free download pdf