public class LinkedBag<E>
extends java.lang.Object
implements java.lang.Cloneable
ArrayBag
,
IntLinkedBag
(2) Because of the slow linear algorithms of this class, large bags will have poor performance.
Constructor and Description |
---|
LinkedBag()
Initialize an empty bag.
|
Modifier and Type | Method and Description |
---|---|
void |
add(E element)
Put a reference to an object into this bag.
|
void |
addAll(LinkedBag<E> addend)
Add the contents of another bag to this bag.
|
LinkedBag<E> |
clone()
Generate a copy of this bag.
|
int |
countOccurrences(E target)
Accessor method to count the number of occurrences of a particular element
in this bag.
|
E |
grab()
Accessor method to retrieve a random element from this bag.
|
Lister<E> |
iterator()
Create an Iterator containing the elements of this bag.
|
boolean |
remove(E target)
Remove one copy of a specified element from this bag.
|
int |
size()
Determine the number of elements in this bag.
|
static <E> LinkedBag<E> |
union(LinkedBag<E> b1,
LinkedBag<E> b2)
Create a new bag that contains all the elements from two other bags.
|
public LinkedBag()
public void add(E element)
element
- the new element to be added to this bagjava.lang.OutOfMemoryError
- Indicates insufficient memory a new Node.public void addAll(LinkedBag<E> addend)
addend
- a bag whose contents will be added to this bagjava.lang.NullPointerException
- Indicates that addend is null.java.lang.OutOfMemoryError
- Indicates insufficient memory to increase the size of the bag.public LinkedBag<E> clone()
clone
in class java.lang.Object
java.lang.OutOfMemoryError
- Indicates insufficient memory for creating the clone.public int countOccurrences(E target)
target
- the element that needs to be countedpublic E grab()
java.lang.IllegalStateException
- Indicates that the bag is empty.public Lister<E> iterator()
public boolean remove(E target)
target
- the element to remove from the bagpublic int size()
public static <E> LinkedBag<E> union(LinkedBag<E> b1, LinkedBag<E> b2)
E
- the type of the elements in this bagb1
- the first of two bagsb2
- the second of two bagsjava.lang.IllegalArgumentException
- Indicates that one of the arguments is null.java.lang.OutOfMemoryError
- Indicates insufficient memory for the new bag.