clayjones
clayjones

Reputation: 13

Trouble casting Arraylist to Double in order to make an array of doubles

I am trying to cast an ArrayList of doubles to an array of doubles. When I try to cast it using the following line:

double[] hist_list = hist_array.Cast<Double>().toArray();

I get the following syntax error:

Syntax error on token "(", Expression expected after this token

and the error is on the first parenthesis, after the Cast. Cast() appears to be an argumentless method in this situation, so what is going wrong here?

Upvotes: 0

Views: 130

Answers (2)

slambeth
slambeth

Reputation: 897

You can't easily convert it to a the primitive type double array, but you can easily convert it to an array of Double[], like so:

Double[] doubleArray = hist_array.toArray(new Double[hist_array.size()]);

Upvotes: 1

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

This is how you can do it:

ArrayList<Double> l = new ArrayList<Double>();
Object[] returnArrayObject = l.toArray();
double returnArray[] = new double[returnArrayObject.length];
for (int i = 0; i < returnArrayObject.length; i++){
     returnArray[i] = (Double)  returnArrayObject[i];
}

Upvotes: 0

Related Questions