1

Is there a way to cast an Object[] array into double[] array without using any loops. And cast Double[] array to double[] array

2

1 Answer 1

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 :|

Sign up to request clarification or add additional context in comments.

1 Comment

Better late than never :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.