bag.java
来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 68 行
JAVA
68 行
/** Bag Class * Author: David D. Riley * Date: June, 2004 * * class invariant * A Bag object ... * * is a container of zero or more other objects * * is capable of storing anything that conforms to Object */public class Bag<ItemType> extends SimpleList<ItemType> { /** post: this is an empty bag (i.e. size() == 0) */ public Bag() { super(); } /** post: result == number of items in this Bag * note: this method is inherited */// public int size() /** post: this == this@pre with some z inserted * and size() == size()@pre + 1 * note: this method is inherited */// public void add(ItemType z) /** pre: count() > 0 <br> * post: this == this@pre with some item removed * and size() == size()@pre - 1 * note: this method is inherited */// public void remove() /** pre: size() > 0 * post: result == one of the items from this Bag */ public ItemType item() { int skipCount; super.reset(); // the following code randomizes which item is returned. skipCount = (int) (Math.random() * size()); for ( ; skipCount>0; skipCount--) { super.next(); } return super.next(); } public String toString() { String str = ""; super.reset(); while (super.hasNext()) { str = str + " " + super.next().toString(); } return str; } // The methods below are overridden to disable them. /** post: this method is disabled */ public void reset() { } /** post: this method is disabled */ public ItemType next() { return null; } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?