title: Kotlin: Generic Collection to Generic Array
body: <p>How to get a generic <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/" rel="noreferrer">array</a> out of a generic <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html" rel="noreferrer">collection</a>?</p>
  
  <p>I have done the following:</p>
  
  <pre><code>val genericArray: Array&lt;E&gt; = (genericCollection as java.util.Collection&lt;E&gt;).toArray() as Array&lt;E&gt;
  </code></pre>
  
  <p>Is this the right way to go or is there a more elegant solution?</p>
  
  <p>Looking forward to your help!</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I ended up writing this helper extension:</p>
  
  <pre><code>fun &lt;E&gt; Collection&lt;E&gt;.toUntypedArray(): Array&lt;E&gt; {
      @Suppress("UNCHECKED_CAST")
      return arrayOf(size, this) as Array&lt;E&gt;
  }
  </code></pre>
  
  <p>Does anyone of you have a better solution for this problem?</p>