I’m having some trouble to do the following:
int[] tmpIntList = (int[])MyArrayList.toArray(someValue);
MyArrayList contains only numbers. I get a typecast error since ArrayList only returns Object[] but why isnt it possible to turn it into a int[] ?
An
ArrayListcan’t containintvalues, as it’s backed by anObject[](even when appropriately generic).The closest you could come would be to have an
Integer[]– which can’t be cast to anint[]. You have to convert each element separately. I believe there are libraries which implement these conversions for all the primitive types.Basically the problem is that Java generics don’t support primitive types.