13

I'm trying to copy the contents of my int array into an array of type double. Do I have to cast them first?

I successfully copied an array of type int to another array of type int. However now I want to write code that would copy contents from Array A to Array Y (int to double).

Here is my code:

public class CopyingArraysEtc {

    public void copyArrayAtoB() {
        double[] x = {10.1,33,21,9},y = null;
        int[] a = {23,31,11,9}, b = new int[4], c;

        System.arraycopy(a, 0, b, 0, a.length);

        for (int i = 0; i < b.length; i++)
        {
            System.out.println(b[i]);
        }

    }          

    public static void main(String[] args) {
        //copy contents of Array A to array B
        new CopyingArraysEtc().copyArrayAtoB();
    }
}
2
  • 1
    Have you tried running it? does it work? do you get any errors? Not entirely sure what the question is. Commented Oct 4, 2012 at 14:18
  • Similar: Convert integer array in double array Commented Feb 26 at 23:30

4 Answers 4

28

Worth mentioning that in this day and age, Java 8 offers an elegant one-liner to do this without the need to use third-party libraries:

int[] ints = {23, 31, 11, 9};
double[] doubles = Arrays.stream(ints).asDoubleStream().toArray();
Sign up to request clarification or add additional context in comments.

1 Comment

This could be a performance hog if you do that thousands/millions of times. Creating many streams of only a few items can degrade performance.
13

System.arraycopy() can't copy int[] to double[]

How about using google guava:

int[] a = {23,31,11,9};

//copy int[] to double[]
double[] y=Doubles.toArray(Ints.asList(a));

Comments

11

You can iterate through each element of the source and add them to the destination array. You don't need an explicit cast going from int to double because double is wider.

int[] ints = {1, 2, 3, 4};
double[] doubles = new double[ints.length];
for(int i=0; i<ints.length; i++) {
    doubles[i] = ints[i];
}

You can make a utility method like this -

public static double[] copyFromIntArray(int[] source) {
    double[] dest = new double[source.length];
    for(int i=0; i<source.length; i++) {
        dest[i] = source[i];
    }
    return dest;
}

1 Comment

Brilliant, thanks for taking the time to explain that :) Its exactly what i was after.
6

From System.arraycopy JavaDoc

[...] Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:

*...

*...

*The src argument and dest argument refer to arrays whose component types are different primitive types. [...]

Since int and double are different primitive types you will have to manually iterate through one array and copy its content to another one.

Comments

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.