1: /** Creates an empty bag having a given capacity.
2: @param desiredCapacity The integer capacity desired. */
3: public ArrayBag(int desiredCapacity)
4: {
5: if (desiredCapacity <= MAX_CAPACITY)
6: {
7: // The cast is safe because the new array contains null entries
8: @SuppressWarnings("unchecked")
9: T[] tempBag = (T[])new Object[capacity]; // Unchecked cast
10: bag = tempBag;
11: numberOfEntries = 0;
12: initialized = true;
13: }
14: else
15: throw new IllegalStateException("Attempt to create a bag " +
16: "whose capacity exceeds " +
17: "allowed maximum.");
18: } // end constructor