【Java开源代码栏目提醒】:本文主要为网学会员提供Bag.java,希望对需要Bag.java网友有所帮助,学习一下!
/** 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;
}
}