Copy contents of an int array to a double array in Java?

前端 未结 4 1527
无人及你
无人及你 2021-01-04 20:35

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 a

4条回答
  •  无人及你
    2021-01-04 21:03

    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

    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

提交回复
热议问题