Vectors : Converting to double[ ] array
Hello,
I have a vector which I know consists of double values only. I am trying to create an array of double from this vector but am having some difficulty.
Firstly if I try this :
double[] high = (double[])datavector.high.toArray(newdouble[0]);
JBuilder reports :
'Cannot find method toArray(double[])'
But if I try this :
Double[] high = (Double[])datavector.high.toArray(new Double[0]);
It works.
So from this I assume 'Double' is not equal to 'double'
The trouble is I require 'double[ ]' and NOT 'Double [ ]'.
Casting Double as (double) does not work... so how do I get double[] from my original vector ?
Many thanks
Kerry
[891 byte] By [
kerrya] at [2007-11-26 15:38:49]

double[] d = new double[v.size()];
int i = 0;
for(Iterator<Double> it = v.iterator(); it.hasNext();)
{
d[i++] = (double)it.next();
}
just declare the double array to be the size of the vector, then loop thru and populate the array one at a time
~Tim
> I'd get an array of doubles if I just cast to :
>
> (double)
>
> Surely that is what the method is provided to do..
> cast to the type I require ?
Casting is not magic conversion. When you cast to Double it works because they ARE Doubles. But casting to double is a problem because they are not doubles they are Doubles.
Ok I understand now that double is not Double.. but I'm still having problems getting a double from the vector.
If I try this :
Double[] highDBL = (Double[])data.high.toArray(new Double[0]);
Then I get an ArrayStoreException.
I can only use 'toArray' to create an array of Objects, but how do I then get an object to double ?