Queue.ToArray into float []?

  • Thread starter Thread starter troy anderson
  • Start date Start date
T

troy anderson

I have a System.Collections.Queue instance filled with floats and I would
like to convert it into a float [] because a third party plotting package
requires it. I have tried casting Queue.ToArray() via

float [] a = (float []) Queue.ToArray();
float [] a = Queue.ToArray() as float[]

no luck. Do I have to extract out each element from the Queue and place
them into my own allocated array?

float [] a = new float[Queue.Count];
int i=0;
foreach(float v in Queue) { a = v; i++;}

Is this very inefficient?

Thanks
 
My understanding is that the ToArray method of the Queue class returns
an object array.

Object[] myStandardArray = mySourceQ.ToArray();

with that in hand, you ought to be able to do:
(float)myStandardArray
 
troy said:
I have a System.Collections.Queue instance filled with floats and I would
like to convert it into a float [] because a third party plotting package
requires it. I have tried casting Queue.ToArray() via

float [] a = (float []) Queue.ToArray();
float [] a = Queue.ToArray() as float[]

no luck. Do I have to extract out each element from the Queue and place
them into my own allocated array?

float [] a = new float[Queue.Count];
int i=0;
foreach(float v in Queue) { a = v; i++;}

Is this very inefficient?

Thanks


Did you check out the CopyTo method?

John Davison
Compass Engineering Group
 
The problem is that your Queue is holding "boxed floats" (value-type
floats converted in refernce-type objects). ToArray (and I suspect CopyTo)
is just copying to copy that objects without unboxing them. However the
float[] needs them unboxed.
float [] a = new float[Queue.Count];
int i=0;
foreach(float v in Queue) { a = v; i++;}

Is this very inefficient?


No, that's fine. It's pretty much just what ToArray is doing.

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

troy anderson said:
I have a System.Collections.Queue instance filled with floats and I would
like to convert it into a float [] because a third party plotting package
requires it. I have tried casting Queue.ToArray() via

float [] a = (float []) Queue.ToArray();
float [] a = Queue.ToArray() as float[]

no luck. Do I have to extract out each element from the Queue and place
them into my own allocated array?

float [] a = new float[Queue.Count];
int i=0;
foreach(float v in Queue) { a = v; i++;}

Is this very inefficient?

Thanks
 
Back
Top