4

i'm looking to cast an int array to a double one.

so, when i have

int arr[] = {1, 2, 3}; 

i want to use arr, say pass it as a double[] param to a method.

what's the best way of doing this?

the cast

(double[])arr

doesn't work.

i can iterate thru arr:

double[] dblA = new double[arr.length];
int ind=0; 
for (int x:arr)
    dblA[ind++]=x;   // (double)x; 

is there a better way of doing this?

System.arraycopy isn't doing it-- doesn't work on arrays of two different primitive types.

Note: saw Casting Object to Array in Java and some other discussions.

TIA.

6
  • Look here : [stackoverflow.com/questions/9710497/… [1]: stackoverflow.com/questions/9710497/… Commented Sep 6, 2014 at 19:45
  • thx for the useful notes. C does these things. a bit discomforting that Java doesn't have t on arrays. Commented Sep 6, 2014 at 19:48
  • 2
    "C does these things"? I don't know of any way you can do this in C without looping through the array and creating a new array by converting each integer to a double. Did I miss something in C? (If you're talking about STL algorithms in C++, I think there might be an equivalent in Java.) Commented Sep 6, 2014 at 19:52
  • @ajb System.arraycopy() doesn't have it. nothing in Arrays either. canèt think of anywhere else in the APIs whetre this can be. Commented Sep 6, 2014 at 20:03
  • 2
    You can cast anything to anything in C. You'll get complete rubbish but you can cast it. :) Commented Sep 6, 2014 at 20:53

2 Answers 2

2

Using Java 7 or below there is no easy way around it, you will simply have to manually copy them over.

Using Java 8 you can do it somewhat easier (you do need to loop over the array of course) using streams like:

int[] intArray = {1, 2, 3};
double[] doubleArray = Arrays.stream(intArray)
    .mapToDouble(i -> i)
    .toArray();

This will first create an IntStream, then map it to a DoubleStream using the conversion IntToDoubleFunction which maps every int i do a corresponding double i. Lastly you collect it using toArray

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

1 Comment

You can use asDoubleStream() instead: Arrays.stream(integerArray).asDoubleStream().toArray();.
1

With Java 8 there is elegant solution based on closure:

package com.java.se.stackoverflow;

import java.util.Arrays;

public class ArrayCast {

    public static void main(String[] argv) throws Exception {
        int[] inputArray = new int[]{23, 4, 5, 6, 89};
        double[] outputArray = new double[inputArray.length];
        Arrays.setAll(outputArray, inputArrayIndex -> (double) inputArray[inputArrayIndex]);

        System.out.println(Arrays.toString(outputArray));

    }

}

6 Comments

What does the given IntToDoubleFunction do?
it converts each integer element and returns double value
Which integer element?
sorry my mistake. Given IntToDoubleFunction function gets index of each element in int array and returnes converted to double
How is this more "elegant" than the OP's original loop? It's more obscure, for sure, if you call that "elegant", and longer.
|

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.