Arraylist of arrays

W

wolima

I have to create a arraylist of arrays. So, at the end, I have to
convert this arraylist in a two dimmensional array.

I wrote something like this.


ArrayList myAL = new ArrayList();
double[] cvt = new double[3];

cvt[0] = 0.0; cvt[1] = 1.01; cvt[2] = 2.02;
myAL.Add(cvt.Clone());

cvt[0] = 10.0; cvt[1] = 11.11; cvt[2] = 22.22;
myAL.Add(cvt.Clone());

double[] myInt = (double[]) myAL.ToArray(typeof(double[]));

But something is wrong when I convert the arraylist in a two
dimmensional array.

Is there a better way to do that?
What is wrong in the convertion line?

Thanks
 
M

Morten Wennevik [C# MVP]

Hi Wolima,

Your last line is wrong. myAl contains a list of double[]. When you are calling ToArray() on myAl it doesn't return just a single double[] butan array of them. Change the last line to

double[][] myInt = (double[][])myAL.ToArray(typeof(double[]));



I have to create a arraylist of arrays. So, at the end, I have to
convert this arraylist in a two dimmensional array.

I wrote something like this.


ArrayList myAL = new ArrayList();
double[] cvt = new double[3];

cvt[0] = 0.0; cvt[1] = 1.01; cvt[2] = 2.02;
myAL.Add(cvt.Clone());

cvt[0] = 10.0; cvt[1] = 11.11; cvt[2] = 22.22;
myAL.Add(cvt.Clone());

double[] myInt = (double[]) myAL.ToArray(typeof(double[]));

But something is wrong when I convert the arraylist in a two
dimmensional array.

Is there a better way to do that?
What is wrong in the convertion line?

Thanks
 
W

wolima

Thanks Morten, for your help.



Hi Wolima,

Your last line is wrong. myAl contains a list of double[]. When you are calling ToArray() on myAl it doesn't return just a single double[] but an array of them. Change the last line to

double[][] myInt = (double[][])myAL.ToArray(typeof(double[]));





I have to create a arraylist of arrays. So, at the end, I have to
convert this arraylist in a two dimmensional array.
I wrote something like this.
ArrayList myAL = new ArrayList();
double[] cvt = new double[3];
cvt[0] = 0.0; cvt[1] = 1.01; cvt[2] = 2.02;
myAL.Add(cvt.Clone());
cvt[0] = 10.0; cvt[1] = 11.11; cvt[2] = 22.22;
myAL.Add(cvt.Clone());
double[] myInt = (double[]) myAL.ToArray(typeof(double[]));
But something is wrong when I convert the arraylist in a two
dimmensional array.
Is there a better way to do that?
What is wrong in the convertion line?

--
Happy coding!
Morten Wennevik [C# MVP]- Ocultar texto entre aspas -

- Mostrar texto entre aspas -
 
A

Alun Harford

wolima said:
I have to create a arraylist of arrays. So, at the end, I have to
convert this arraylist in a two dimmensional array.

I wrote something like this.
double[] myInt = (double[]) myAL.ToArray(typeof(double[]));

As Morten said, in this line you want to be casting to double[][].

It should be pointed out that ArrayList is basically obsolete. If you
had used List<double[]> instead, you would have got a nice meaningful
compiler error. :)

Alun Harford
 

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