Solved

# How to cast an ArrayList to a double[] array?

Posted on 2005-05-17
549 Views
double[] costArray;
...
costArray = callToDoSomething(paramaters...);
---------------------------

As you can see the costArray is a double array, and the "callToDoSomething" method returns an ArrayList.

I need costArray to be of type double[] but in the 'callToDoSomething' method I have to return an arraylist because the contents are dynamically added.

Any suggestions on casting/converting to an array from the arraylist?
0
Question by:djs120

LVL 9

Accepted Solution

ArrayList costArray = callToDoSomething(...);

double[] costArray2 = new double[costArray.Length];
costArray.CopyTo(costArray2);
0

LVL 1

Author Comment

I noticed there is a .ToArray() method for ArrayLists, would this be helpful to me at all?
0

LVL 1

Author Comment

And where you say to do:

double[] costArray2 = new double[costArray.Length];

I can't get the .Length property on the ArrayList.  Suggestions?
0

LVL 3

Expert Comment

To get the number of objects in an ArrayList you have to query the 'Count' property.
Anyway, AFAIK, the CopyTo method only works with an Array class as destination, and not with a 'pure' array.
So I guess you're stuck with using something like:

ArrayList AL = new ArrayList();
//Populate ArrayList
double[] A = new double[AL.Count];
for (int I=0; I <= AL.Count - 1; I++) {
A[I] = Convert.ToDouble(AL[I]);
}
0

LVL 9

Expert Comment

Oops, uKER is correct; use ArrayList.Count.

And a "pure" array is derived from the Array class, so yes, it will work with the CopyTo() method.
0

LVL 1

Author Comment

Thanks guys.
0

LVL 3

Expert Comment

BurntSky is right, I checked and it does work. Only check you explicitly add the objects as integers.
The following code won't work, as there is no explicit conversion when copying the elements to the array:

ArrayList AL = new ArrayList();
AL.AddRange(new object[] { 1, 2, 3 });
double[] A = new double[AL.Count];
AL.CopyTo(A);

This is because 1, 2 and 3 are integer values and implicitly assigning them to the array's double values generates an exception.
This particular case could be fixed by specifying the array as {1.0, 2.0, 3.0}
0

## Featured Post

C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
This video discusses moving either the default database or any database to a new volume.