【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:AscendingList.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
// Figure 11.18
/** CLass Invariant
* An AscendingListOfInt contains objects sorted in ascending order.
*/
public class AscendingList<ItemType extends Comparable> extends SimpleList<ItemType> {
/** post: this is empty
* and iterator == 0
*/
public AscendingList() {
super();
}
/** post: this list == this@pre with z inserted (note that the
* sort property from the class invariant is maintained)
* and iterator is positioned immediately after
* the newly inserted z
*/
public boolean add(ItemType z) {
boolean found;
ItemType temp = null;
reset();
found = false;
while ( !found && hasNext() ) {
temp = next();
found = (z.compareTo( temp ) <= 0);
}
if (!found) {
super.add(z);
} else {
remove();
super.add(z);
super.add(temp);
}
return true; // because add is a boolean method
}
}