How I can do that?
I have an arraylist, with float elements. (Arraylist <Float>)
(float[]) Floats_arraylist.toArray()
it is not working.
cannot cast from Object[] to float[]
How I can do that?
I have an arraylist, with float elements. (Arraylist <Float>)
(float[]) Floats_arraylist.toArray()
it is not working.
cannot cast from Object[] to float[]
You can use Apache Commons
ArrayUtils.toPrimitive()
:Loop over it yourself.
The nullcheck is mandatory to avoid
NullPointerException
because aFloat
(an object) can benull
while afloat
(a primitive) cannot benull
at all.In case you're on Java 8 already and it's no problem to end up with
double[]
instead offloat[]
, considerStream#mapToDouble()
(no there's no such method asmapToFloat()
).Apache Commons Lang to the rescue.