title: Bag implementation as array in Java
body: <p>I'm supposed to implement a bag data structure (also called a multiset), an unordered
  collection of homogeneous values (any Java object, excluding null) that may have duplicates, for a project. I've done extensive searching on the internet but have a hard time wrapping my mind around using arrays instead of something like List and don't quite understand the syntax for using arrays in a class.</p>
  
  <p>I need to implement all of java.util.Collection except as noted by throwing an UnsupportedOperationException. Yes, I HAVE to use an array and when I add to it, the capacity must increase by 10. My problem is that I'm not sure what to do about the <i>contains</i> method, <i>clear</i> method, <i>addAll</i> method, and a second <i>constructor</i>. Hopefully everything else I've added will also run smoothly. I've included the API definition in comment blocks. Any input at all would really help me.</p>
  
  <p>As Mark asked below, I don't understand how to search through the bag to search for a particular element.</p>
  
  <pre><code>import java.util.Collection;
  import java.util.Iterator;
  
  class Bag&lt;T&gt; implements Collection&lt;T&gt;{
  private T[] array;
  public int bagSize;
  
  
  public Bag(){
      array=(T[])new Object[10];
  }
  public Bag(Collection&lt;T&gt; other ){
      //Not sure what to put here
      //creates a bag containing all of the items passed to it as a Collection&lt;T&gt;
  }
  
  public int size() {
      return bagSize; 
  }
  
  public boolean isEmpty() {
      if(size()==0)
          return true;
      else
          return false;
  }
  
  
  public boolean contains(Object o) {
      //Not sure what to put here
      /*Returns true if this collection contains the specified element. More formally,
      returns true if and only if this collection contains at least one element e such 
      that (o==null ? e==null : o.equals(e)). */
      return (o.toArray()==null ? this.toArray()==null : o.toArray() == this.toArray());
      }
  
  }
  
  
  public Iterator&lt;T&gt; iterator() {
      throw new UnsupportedOperationException("not implemented.");
  }
  
  public Object[] toArray() {
      return array;
  
  }
  
  public &lt;T&gt; T[] toArray(T[] a) {
      throw new UnsupportedOperationException("not implemented.");
  }
  
  public boolean add(T e) {
     if(bagSize&gt;=array.length)
         return false;
     else
     {
         ensureCapacity(bagSize+10);
         array[bagSize]=e;
         bagSize++;
         return true;
     }
  
  }
  
  public boolean remove(Object o) {
      for(int i=0; i&lt;bagSize; i++)
          if(array[i].equals(o)){
              for(int j=i; j&lt;bagSize-1; j++)
                  array[j]=array[j+1];
              bagSize--;
              return true;
          }
      return false;
  
  }
  
  public boolean containsAll(Collection&lt;?&gt; c) {
      throw new UnsupportedOperationException("not implemented.");
  }
  
  public boolean addAll(Collection&lt;? extends T&gt; c) {
      //Not sure what to put here
      /*Adds all of the elements in the specified collection to this collection  
      (optional operation). The behavior of this operation is undefined if the specified
      collection is modified while the operation is in progress. (This implies that the
      behavior of this call is undefined if the specified collection is this collection,
      and this collection is nonempty.) */
  }
  
  public boolean removeAll(Collection&lt;?&gt; c) {
      throw new UnsupportedOperationException("not implemented.");
  }
  
  public boolean retainAll(Collection&lt;?&gt; c) {
      throw new UnsupportedOperationException("not implemented.");
  }
  
  public void clear() {
      //Not sure what to put here
      /*Removes all of the elements from this collection (optional operation). The
      collection will be empty after this call returns (unless it throws an exception).*/
  }
  
  @Override
  public int hashCode(){
      throw new UnsupportedOperationException("not implemented.");
  }
  
  @Override
  public boolean equals(Object e) {
      if (e == null) {
          return false;
      }
      if (getClass() != e.getClass()) {
          return false;
      }
      final Bag&lt;T&gt; other = (Bag&lt;T&gt;) e;
      return true;
  }
  
  public void ensureCapacity(int minCapacity){
      T[] biggerArray;
      if(array.length&lt;minCapacity){
          biggerArray=(T[]) new Object[minCapacity];
          System.arraycopy(array, 0, biggerArray, 0, bagSize);
          array=biggerArray; 
      }
  }
  </code></pre>