I keep getting an out-of-bounds-exception. I've checked other sources and it mostly seems to be due to a faulty loops, but mine is fine.
Both arrays _store and arr have entries.
HEADER CODE:
public class ArrayMultiSet<E> implements Collection<E> {
/** Unless otherwise specified, the default length to which the backing store should be initialized. */
private static final int DEFAULT_INITIAL_CAPACITY = 16;
/** Array in which the elements in this multiset are stored. */
private E[] _store;
/**
* Array indices below this amount contain the active elements in this collection.
*/
private int _size;
/**
* Create a new empty multiset.
*/
public ArrayMultiSet() {
clear();
}
MIDDLE QUESTION:
/**
* Resets the Multiset so that it only contains the entries in the given array. This overwrites the data previously in
* the Collection.
*
* @param arr The array whose entries will be the elements in the Collection
*/
@SuppressWarnings("unchecked")
public void fromArray(E[] arr) {
// IMPORTANT: You CANNOT set the backing store to be equal to ("alias")
// arr. If you did this, the calling method could make changes to the
// Multiset by updating the entries in arr rather than using the Multiset methods.
// This violates good OO practice and creates the potential for bugs and hacks.
_size = arr.length;
int i = 0;
while (i < _size) {
_store[i] = arr[i];
i++;
}
}
JUNIT: