title: How can I convert List<Integer> to int[] in Java?
body: <p>I'm new to Java. How can I convert a <code>List&lt;Integer&gt;</code> to <code>int[]</code> in Java?</p>
  <p>I'm confused because <code>List.toArray()</code> actually returns an <code>Object[]</code>, which can be cast to neither <code>Integer[]</code> nor <code>int[]</code>.</p>
  <p>Right now I'm using a loop to do so:</p>
  <pre><code>int[] toIntArray(List&lt;Integer&gt; list) {
    int[] ret = new int[list.size()];
    for(int i = 0; i &lt; ret.length; i++)
      ret[i] = list.get(i);
    return ret;
  }
  </code></pre>
  <p>Is there's a better way to do this?</p>
  <p><sub>This is similar to the question
  <em><a href="https://stackoverflow.com/questions/880581/java-convert-int-to-integer">How can I convert int[] to Integer[] in Java?</a></em>.</sub></p>