/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package week3; import java.util.Arrays; /** * * @author roxana */ public class ArrayBag implements BagInterface { private final T[] bag; private static final int DEFAULT_CAPACITY=25; private int numberOfEntries; public ArrayBag() { this(DEFAULT_CAPACITY); } public ArrayBag(int capacity){ numberOfEntries =0; T[] tempBag =(T[])new Object[capacity]; bag =tempBag; } public boolean add(T newEntry){ boolean result =true; if (isFull()) { result =false; } else { bag[numberOfEntries]=newEntry; numberOfEntries++; } return result; } public int getIndexOf(T anEntry){ int index =-1; for ( index=0; index=0)) { result =bag[index]; numberOfEntries--; bag[index] =bag[numberOfEntries]; bag[numberOfEntries]=null; } return result; } public T[] toArray(){ @SuppressWarnings("unchecked") T[] result =(T[])new Object[numberOfEntries]; for (int index=0; index=0) { bag[index] =newItem; return true; } return false; } public void removeAll(T item) { while (this.contains(item)) this.remove(item); } public boolean equals(BagInterface other) { T[] thisBag =this.toArray(); T[] otherBag =other.toArray(); Arrays.sort(thisBag); Arrays.sort(otherBag); if (thisBag.length != otherBag.length) return false; for (int index =0; index