title: Implementing a Arraylist-based Bag/Multiset- stuck on generics
body: <p>An assignment has asked us to implement a Bag data type using either ArrayList or LinkedList as the backend.  I've picked the ArrayList and created my own implementation of it, including local implementations of the Collection interface.  Now I'm struggling with the basic concept of how to adapt the properties of a Bag using the my List class, specifically inserting a List within a List.</p>
  
  <p>The way my List is set up follows the usual Java type, except all the methods aren't implemented, which is why I wrote my own Collection interface type as</p>
  
  <pre><code>public interface Collection&lt;E&gt;
  </code></pre>
  
  <p>implementing add, remove, size, clear, contains, isEmpty and toArray methods.</p>
  
  <p>The List type is declared as </p>
  
  <pre><code>public class List&lt;E&gt; implements Collection&lt;E&gt; 
  </code></pre>
  
  <p>with three private fields:</p>
  
  <pre><code>private int size;
  private E[] data;
  private int growSize=10;
  </code></pre>
  
  <p>the <code>E[] data</code> holds whatever element type is needed, and growSize is the number of new slots to increase the list size as needed. This works great when the List is used traditionally for single objects, but I get errors when I try to insert a List type.</p>
  
  <p>I'm trying to implement this method:</p>
  
  <pre><code>public boolean add(Collection&lt;E&gt; c){
    if (size &lt; data.length){
      data= (E[]) new Object[10];
      data[0]= c;
    }
  }
  </code></pre>
  
  <p>When I try this, it says there's a type mismatch between Collection and E. </p>
  
  <p>Two questions:</p>
  
  <ul>
  <li>First, what rules of Java generics am I breaking here and why?</li>
  <li>Second, what is needed to fix it?</li>
  </ul>