title: Java List <T> T[] toArray(T[] a) implementation
body: <p>I was just looking at the method defined in the List interface: <code>&lt;T&gt; T[] toArray(T[] a)</code>
  , and I have a question. Why is it generic? Because of that fact, method is not complete    type-safe. The following code fragment compiles but causes <code>ArrayStoreException</code>:</p>
  
  <pre><code>List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;();
  list.add(1);
  list.add(2);
  
  String[] stringArray = list.toArray(new String[]{});
  </code></pre>
  
  <p>It seems to me if toArray was not generic and took List type parameter, it would be better.</p>
  
  <p>I have written toy example and it is ok witout generic:</p>
  
  <pre><code>package test;
  
  import java.util.Arrays;
  
  public class TestGenerics&lt;E&gt; {
      private Object[] elementData = new Object[10];
  private int size = 0;
  
      public void add(E e) {
      elementData[size++] = e;
  }
  
  @SuppressWarnings("unchecked")
      //I took this code from ArrayList but it is not generic
  public E[] toArray(E[] a) {
      if (a.length &lt; size)
          // Make a new array of a's runtime type, but my contents:
          return (E[]) Arrays.copyOf(elementData, size, a.getClass());
      System.arraycopy(elementData, 0, a, 0, size);
      if (a.length &gt; size)
          a[size] = null;
      return a;
  }
  
      public static void main(String[] args) {
  
      TestGenerics&lt;Integer&gt; list = new TestGenerics&lt;Integer&gt;();
      list.add(1);
      list.add(2);
      list.add(3);
      //You don't have to do any casting
      Integer[] n = new Integer[10];
      n = list.toArray(n);
  }
  }
  </code></pre>
  
  <p>Is there any reason why it is declared that way?</p>