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.

Community
  • 1
  • 1
user3880721
  • 613
  • 6
  • 16
  • Look here : [http://stackoverflow.com/questions/9710497/shortest-way-to-cast-a-2d-int-array-into-2d-double][1] [1]: http://stackoverflow.com/questions/9710497/shortest-way-to-cast-a-2d-int-array-into-2d-double – liotims Sep 06 '14 at 19:45
  • thx for the useful notes. C does these things. a bit discomforting that Java doesn't have t on arrays. – user3880721 Sep 06 '14 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.) – ajb Sep 06 '14 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. – user3880721 Sep 06 '14 at 20:03
  • You still haven't answered my question: where does C have anything like this? There are some C++ functions where you could copy from one collection to another and provide a transformation function that operates on each element; but Java 8 has that ability too (using lambdas). – ajb Sep 06 '14 at 20:08
  • 2
    You can cast anything to anything in C. You'll get complete rubbish but you can cast it. :) – biziclop Sep 06 '14 at 20:53

2 Answers2

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

skiwi
  • 66,971
  • 31
  • 131
  • 216
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));

    }

}