title: Casting Objects to <T>-type
body: <p>I'm implementing List interface to a class which stores data in a <code>&lt;T&gt;</code> type array. This raises problems with methods that take Collection as parameters, since collection stores it's objects as <code>Object</code>. How can I transform <code>Object</code> to <code>T</code> ? Simple casting doesn't work.</p>
  
  <pre><code>class MyCollection&lt;T&gt; implements List&lt;T&gt;{
      T[] tab;
  
      MyCollection() {
          this.tab = (T[])new Object[10];
      }
  
      public boolean addAll(Collection c){
          for(Object o : c){
              o = (T)o;
              for(int i = 0; i &lt; tab.length; i++){
                  tab[i] = o;
              }
          }
      }
  
  }
  </code></pre>
  
  <p>I'm trying :</p>
  
  <pre><code>public boolean addAll(Collection&lt;? extends T&gt; c)
  </code></pre>
  
  <p>but it fails with :</p>
  
  <p><code>public boolean retainAll(Collection&lt;?&gt; c)</code> since I cannot change the Collection type here :/</p>
  
  <pre><code>public boolean retainAll(Collection&lt;?&gt; c){
      boolean result = false;
      for(T t : c){
      }
      return result;
  }
  </code></pre>