Item
- the generic type of an item in this bagpublic class Bag<Item>
extends java.lang.Object
implements java.lang.Iterable<Item>
Bag
class represents a bag (or multiset) of
generic items. It supports insertion and iterating over the
items in arbitrary order.
This implementation uses a singly-linked list with a static nested class Node.
See LinkedBag
for the version from the
textbook that uses a non-static nested class.
The add, isEmpty, and size operations
take constant time. Iteration takes time proportional to the number of items.
For additional documentation, see Section 1.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
Bag()
Initializes an empty bag.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Item item)
Adds the item to this bag.
|
boolean |
isEmpty()
Returns true if this bag is empty.
|
com.example.colibris.multi.graph.Bag.ListIterator<Item> |
iterator()
Returns an iterator that iterates over the items in this bag in arbitrary order.
|
void |
set(int i,
Item e) |
int |
size()
Returns the number of items in this bag.
|
public boolean isEmpty()
true
if this bag is empty;
false
otherwisepublic int size()
public void add(Item item)
item
- the item to add to this bagpublic void set(int i, Item e)
public com.example.colibris.multi.graph.Bag.ListIterator<Item> iterator()
iterator
in interface java.lang.Iterable<Item>