public class ResizingArrayBag<Item> extends java.lang.Object implements java.lang.Iterable<Item>
This implementation uses a resizing array.
See LinkedBag
for a version that uses a singly-linked list.
The add operation takes constant amortized time;
the 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 |
---|
ResizingArrayBag()
Initializes an empty bag.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Item item)
Adds the item to this bag.
|
boolean |
isEmpty()
Is this bag empty?
|
java.util.Iterator<Item> |
iterator()
Returns an iterator that iterates over the items in the bag
in arbitrary order.
|
static void |
main(java.lang.String[] args)
Unit tests the ResizingArrayBag data type.
|
int |
size()
Returns the number of items in this bag.
|
public boolean isEmpty()
public int size()
public void add(Item item)
public java.util.Iterator<Item> iterator()
iterator
in interface java.lang.Iterable<Item>
public static void main(java.lang.String[] args)