Possible Duplicate:
How to convert an ArrayList containing Integers to primitive int array?
I tried the code below, but it does not work. Do I have to make a loop copy the ArrayList into an array ?
int[]intArray = (int[]) integerArrayList.toArray();
If you want primitive type array, you can use
Apache CommonsArrayUtils#toPrimitivemethod to get primitive array from wrapper array: –You can use it like this: –
Or, you can use the 1-arg version of toArray method, that takes an array and returns the array of that type only. That way, you won’t have to the typecasting.
However, if you are Ok with the
wrapper typearray (Which certainly will not trouble you), then you don’t need to do that last step.Also, you can also create a primitive array, without having to create that intermediate Wrapper type array. For that you would have to write it through
loop: –