10

I have an ArrayList called out, and I need to convert it to a double[]. The examples I've found online have said two things:

First, try:

double[] d = new double[out.size()];
out.toArray(d);

However, this produces the error (eclipse):

The method toArray(T[]) in the type List<Double> is not applicable for the arguments (double[]).

The second solution I found was on StackOverflow, and was:

double[] dx = Arrays.copyOf(out.toArray(), out.toArray().length, double[].class);

However, this produces the error:

The method copyOf(U[], int, Class<? extends T[]>) in the type Arrays is not applicable for the arguments (Object[], int, Class<double[]>)

What is causing these errors, and how do I convert out to double[] without creating these problems? out indeed holds only double values.

Thanks!

3
  • how you define out? must it be double[] or Double[] is acceptable? Commented Jan 3, 2013 at 7:14
  • 1
    Uh... downvote, why? Research was made, multiple things were attempted, and I still had a question. Commented Jan 3, 2013 at 7:20
  • @Emrakul I did not vote up or down, but a SSCCE would make this question much better IMO. Commented May 12, 2016 at 3:02

3 Answers 3

12

I think you are trying to convert ArrayList containing Double objects to primitive double[]

public static double[] convertDoubles(List<Double> doubles)
{
    double[] ret = new double[doubles.size()];
    Iterator<Double> iterator = doubles.iterator();
    int i = 0;
    while(iterator.hasNext())
    {
        ret[i] = iterator.next();
        i++;
    }
    return ret;
}

ALternately, Apache Commons has a ArrayUtils class, which has a method toPrimitive()

 ArrayUtils.toPrimitive(out.toArray(new Double[out.size()]));

but i feel it is pretty easy to do this by yourself as shown above instead of using external libraries.

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

3 Comments

Also works with for(int i = 0; i < ret.length; i++) ret[i] = out.get(i).doubleValue();, no need for an iterator. Thank you!
This won't work. i=i++ results in i always being 0, put simply i++; Also, explicit unboxing (.doubleValue()) is unnecessary.
incorporated the suggested changes. Thanks
2

Have you tried

Double[] d = new Double[out.size()];
out.toArray(d);

i.e use the class Double and not the primitive type double

The error messages seem to imply that this is the issue. After all, since Double is a wrapper class around the primitive type double it is essentially a different type, and compiler will treat it as such.

2 Comments

@Telthien in that case could you redefine the list as List<double>
@Telthien or you could use ArrayUtils as shown in the link to convert from Double[] to double[]
1

Generics does not work with primitive types that's why you are getting an error. Use Double array instead of primitive double. Try this -

Double[] d = new Double[out.size()];
out.toArray(d);
double[] d1 = ArrayUtils.toPrimitive(d);

1 Comment

what is the package for ArrayUtils

Your Answer

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