public class IntLinkedBag
extends java.lang.Object
implements java.lang.Cloneable
IntArrayBag
,
LinkedBag
(2) Because of the slow linear algorithms of this class, large bags will have poor performance.
Constructor and Description |
---|
IntLinkedBag()
Initialize an empty bag.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int element)
Add a new element to this bag.
|
void |
addAll(IntLinkedBag addend)
Add the contents of another bag to this bag.
|
void |
addMany(int... elements)
Add new elements to this bag.
|
java.lang.Object |
clone()
Generate a copy of this bag.
|
int |
countOccurrences(int target)
Accessor method to count the number of occurrences of a particular element
in this bag.
|
int |
grab()
Accessor method to retrieve a random element from this bag.
|
boolean |
remove(int target)
Remove one copy of a specified element from this bag.
|
int |
size()
Determine the number of elements in this bag.
|
static IntLinkedBag |
union(IntLinkedBag b1,
IntLinkedBag b2)
Create a new bag that contains all the elements from two other bags.
|
public IntLinkedBag()
public void add(int element)
element
- the new element that is being addedjava.lang.OutOfMemoryError
- Indicates insufficient memory a new IntNode.public void addAll(IntLinkedBag 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 void addMany(int... elements)
elements
- (a variable-arity argument)
one or more new elements that are being insertedjava.lang.OutOfMemoryError
- Indicates insufficient memory to increase the size of the bag.public java.lang.Object clone()
clone
in class java.lang.Object
java.lang.OutOfMemoryError
- Indicates insufficient memory for creating the clone.public int countOccurrences(int target)
target
- the element that needs to be countedpublic int grab()
java.lang.IllegalStateException
- Indicates that the bag is empty.public boolean remove(int target)
target
- the element to remove from the bagpublic int size()
public static IntLinkedBag union(IntLinkedBag b1, IntLinkedBag b2)
b1
- 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.