title: Convert a generic list to an array
body: <p>I have searched for this, but unfortunately, I don't get the correct answer.</p>
  
  <pre><code>class Helper {
      public static &lt;T&gt; T[] toArray(List&lt;T&gt; list) {
          T[] array = (T[]) new Object[list.size()];
          for (int i = 0; i &lt; list.size(); i++) {
              array[i] = list.get(i);
          }
          return array;
      }
  }
  </code></pre>
  
  <p>Test it:</p>
  
  <pre><code>public static void main(String[] args) {
      List&lt;String&gt; list = new ArrayList&lt;String&gt;();
      list.add("abc");
      String[] array = toArray(list);
      System.out.println(array);
  }
  </code></pre>
  
  <p>But there is an error thrown:</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
  at test.Helper.main(Helper.java:30)
  </code></pre>
  
  <p>How to solve this?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I want this method, because sometimes, the type in my code is too long:</p>
  
  <pre><code>newEntries.toArray(new IClasspathEntry[0])
  </code></pre>
  
  <p>I'd hope to call:</p>
  
  <pre><code>toArray(newEntries)
  </code></pre>
  
  <hr>
  
  <p><strong>FINALLY</strong></p>
  
  <p>It seems impossible to create such a method, thank you all very much!</p>