|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectedu.colorado.collections.ArrayBag<E>
public class ArrayBag<E>
An ArrayBag is a generic collection of references to objects. The same object may appear multiple times in a bag.
IntArrayBag,
LinkedBag(2) A bag's capacity cannot exceed the maximum integer 2,147,483,647 (Integer.MAX_VALUE). Any attempt to create a larger capacity results in a failure due to an arithmetic overflow.
(3) Because of the slow linear algorithms of this class, large bags will have poor performance.
| Constructor Summary | |
|---|---|
ArrayBag()
Initialize an empty bag with an initial capacity of 10. |
|
ArrayBag(int initialCapacity)
Initialize an empty bag with a specified initial capacity. |
|
| Method Summary | ||
|---|---|---|
void |
add(E element)
Add a new element to this bag. |
|
void |
addAll(ArrayBag<E> addend)
Add the contents of another bag to this bag. |
|
void |
addMany(E... elements)
Add new elements to this bag. |
|
ArrayBag<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. |
|
void |
ensureCapacity(int minimumCapacity)
Change the current capacity of this bag. |
|
int |
getCapacity()
Accessor method to get the current capacity of this bag. |
|
E |
grab()
Accessor method to retrieve a random element from 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. |
|
void |
trimToSize()
Reduce the current capacity of this bag to its actual size (i.e., the number of elements it contains). |
|
static
|
union(ArrayBag<E> b1,
ArrayBag<E> b2)
Create a new bag that contains all the elements from two other bags. |
|
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ArrayBag()
- - nonejava.lang.OutOfMemoryError - Indicates insufficient memory for:
new Object[10].public ArrayBag(int initialCapacity)
initialCapacity - the initial capacity of this bagjava.lang.IllegalArgumentException - Indicates that initialCapacity is negative.
java.lang.OutOfMemoryError - Indicates insufficient memory for: new Object[initialCapacity].| Method Detail |
|---|
public void add(E element)
element - the new element that is being insertedjava.lang.OutOfMemoryError - Indicates insufficient memory for increasing the bag's capacity.public void addMany(E... elements)
elements - (a variable-arity argument)
one or more new elements that are being insertedjava.lang.OutOfMemoryError - Indicates insufficient memory for increasing the bag's capacity.public void addAll(ArrayBag<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 ArrayBag<E> clone()
clone in class java.lang.Object- - none
java.lang.OutOfMemoryError - Indicates insufficient memory for creating the clone.public int countOccurrences(E target)
target - the element that needs to be counted
public void ensureCapacity(int minimumCapacity)
minimumCapacity - the new capacity for this bagjava.lang.OutOfMemoryError - Indicates insufficient memory for: new Object[minimumCapacity].public int getCapacity()
- - none
public E grab()
- - nonejava.lang.IllegalStateException - Indicates that the bag is empty.public boolean remove(E target)
target - the element to remove from the bagpublic int size()
- - none
public void trimToSize()
- - nonejava.lang.OutOfMemoryError - Indicates insufficient memory for altering the capacity.
public static <E> ArrayBag<E> union(ArrayBag<E> b1,
ArrayBag<E> b2)
b1 - the first of two bagsb2 - the second of two bagsNullPointerException. - Indicates that one of the arguments is null.
java.lang.OutOfMemoryError - Indicates insufficient memory for the new bag.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||