Is there a way to cast an Object[]
array into double[]
array without using any loops. And cast Double[]
array to double[]
array
Asked
Active
Viewed 1,751 times
1

Barney
- 2,355
- 3
- 22
- 37

Osman Khalid
- 778
- 1
- 7
- 22
-
This answers the second part of your question: http://stackoverflow.com/questions/1109988/how-do-i-convert-double-to-double – Barney Feb 23 '13 at 19:09
-
The above link says, I have to use loop, which I want to avoid. Java definitely failed here in comparison to .net (http://stackoverflow.com/questions/3741350/how-does-c-sharp-generics-affect-collections-with-primitives) – Osman Khalid Feb 26 '13 at 05:55
1 Answers
1
In 2013 we haven't Java Stream API, just in march of 2014. With it you can get your answer:
From Object[] to double[]
Object[] objectArray = {1.0, 2.0, 3.0};
double[] convertedArray = Arrays.stream(objectArray) // converts to a stream
.mapToDouble(num -> Double.parseDouble(num.toString())) // change each value to Double
.toArray(); // converts back to array
From Double[] to double[]
Double[] doubleArray = {1.0, 2.0, 3.0};
double[] conv = Arrays.stream(doubArray)
.mapToDouble(num -> Double.parseDouble(num.toString()))
.toArray();
You notice that it's the same operation, since the resulting type for both conversions are double[]. What is changed is the source data.
PS: what a late answer :|

tomrlh
- 1,006
- 1
- 18
- 39