// ------------------------------------------------------------------------- /** * This ArrayBag collection holds items of a generic data type with no * particular order. Duplicate items are allowed. * * @author Kevin Buffardi and CMPS 1600 students * @version Feb 10, 2014 */ public class ArrayBag implements Collection { private final static int INITIAL_CAPACITY = 10; private T[] contents; private int size; /** * Constructor initializes an empty collection w/ an initial capacity */ public ArrayBag() { contents = (T[])new Object[INITIAL_CAPACITY]; size = 0; } /** * add a new item into the bag (arbitrary order) * @param item - element to be added into the bag */ public void add(T item) { if( contents.length == size ) { resize(); contents[size] = item; size = size+1; } else { contents[size]=item; size = size + 1; } } /** * helper method for expanding the capacity of the bag */ private void resize() { T[] temp = (T[])new Object[size+INITIAL_CAPACITY]; for(int i=0; i