Avatar ·

How to convert List<Integer> to int[] in Java?

Hello everyone! I'm new to Java and ran into a problem. I have a list of integers `List` that I get after processing data. I need to pass it to a method that only accepts a primitive array `int[]`. How can I do this?\n\nI tried just casting the types, but the compiler complains. Here's an example of my code:\n\n```java\nList numbers = new ArrayList<>();\nnumbers.add(10);\nnumbers.add(20);\nnumbers.add(30);\n\n// This doesn't work — compilation error\nint[] array = (int[]) numbers;\n```\n\nI understand that `List` is an object, and `int[]` is a primitive array. But how do I convert one to the other? I tried using `toArray()`:\n\n```java\nInteger[] array = numbers.toArray(new Integer[0]);\n```\n\nBut this gives an `Integer[]` array, and I need exactly `int`