ArrayList ToArray() throwing exception (concatenate double[])

M

Michael Howes

I have many double[] that each have a few thousand numbers in them.
I need to concatenate groups of these double arrays into a new set of
double[].
I don't know the total # of points.

I thought it would be fairly easy to add these different double arrays
to different ArrayLists and then use ToArray to get all the numbers back
in one array. Sort of a cheap concatenate so I don't need to loop
through all the points or keep newing larger arrays

when I call ToArray
(double[])yData.ToArray(typeof(double))

I get an exception; "At least one element in the source array could
not be cast down to the destination array type."

Umm..they were all valid doubles in arrays of doubles before being
added to the ArrayList

any ideas what this might be?

thanks
mike
 
J

Jon Skeet [C# MVP]

Michael Howes said:
I have many double[] that each have a few thousand numbers in them.
I need to concatenate groups of these double arrays into a new set of
double[].
I don't know the total # of points.

I thought it would be fairly easy to add these different double arrays
to different ArrayLists and then use ToArray to get all the numbers back
in one array. Sort of a cheap concatenate so I don't need to loop
through all the points or keep newing larger arrays

when I call ToArray
(double[])yData.ToArray(typeof(double))

I get an exception; "At least one element in the source array could
not be cast down to the destination array type."

Umm..they were all valid doubles in arrays of doubles before being
added to the ArrayList

any ideas what this might be?

Did you add the *arrays* to the ArrayList, or each double in turn? If
you're adding the arrays, then ArrayList isn't going to do any
flattening for you, unless you call AddRange instead of Add.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Well, the message says everything, at least one element could not be
converted to double.

Do manually and see if you find any problem:

foreach( object o in GetElements )
double d = (double) o;

and see if you get an error and where it's located
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Michael said:
I have many double[] that each have a few thousand numbers in them.
I need to concatenate groups of these double arrays into a new set of
double[].
I don't know the total # of points.

Of course you do. Each array has a length, you only have to add them.
I thought it would be fairly easy to add these different double arrays
to different ArrayLists and then use ToArray to get all the numbers back
in one array. Sort of a cheap concatenate so I don't need to loop
through all the points or keep newing larger arrays

Any method that could do what you are trying to accomplish, would do
exactly what you are trying to avoid doing yourself.
when I call ToArray
(double[])yData.ToArray(typeof(double))

I get an exception; "At least one element in the source array could
not be cast down to the destination array type."

Umm..they were all valid doubles in arrays of doubles before being
added to the ArrayList

You can't convert a double array into a double. The ToArray method will
produce an array with the same number of items as the ArrayList has, it
won't expand any arrays in the ArrayList.

Just create an array with the length of the sum of all the arrays that
you want to put in it, then use the CopyTo method to copy the data from
each array into the new array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top