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]
# 1
double is a primitive. Double is an object and it is the object wrapper for double.Your Vector does not hold doubles but it does hold Doubles. Also worth noting that you can get doubles from Doubles with doubleValue()
cotton.ma at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 2
so, you cast to Double and then iterate through the array and for each element do .toDoubleValue()(You'll have to create a double[] array
manuel.leiriaa at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 3

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

SomeoneElsea at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks for the replies, but why can't I use : '<T> T[] toArray(T[] a)' ...method of the Vector class ? Surely this allows me to cast to the type I desire without having to iterate through the whole of the vector ?
kerrya at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 5
You could do that, but you'll still get an array of Doubles, not doubles. That still leaves the problem of getting those values into a double array.
hunter9000a at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 6
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 ?
kerrya at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 7

> 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.

cotton.ma at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 8
Because when you said the Vector has doubles you are wrong. It has Doubles. Vectors cannot hold primitives, they hold objects. I think largely the problem is that you have been snake-bitten by autoboxing.
cotton.ma at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 9

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 ?

kerrya at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...
# 10
Assuming that data.high is a vector of Double, then you need to pass a properly dimensioned array to the toArray method.Double[] highDBL = (Double[])data.high.toArray(new Double[data.high.size()]);~Tim
SomeoneElsea at 2007-7-8 21:57:06 > top of Java-index,Java Essentials,Java Programming...