Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi How would I populate the elements of an array from a list


instead of this

C#
double[][] items ={
         new double[3] {.05,.05,0},
         new double[3] {0,0,1},
         new double[3]{.1,.05,1}};

Elements are in a arraylist
new double[3]{i,i++,i++}?
Posted
Comments
LanFanNinja 12-Dec-11 18:58pm    
Check my solution.

1 solution

Something like this should work for you.

C#
ArrayList alist = new ArrayList();
alist.Add(new double[] { 3.0, 5.1, 4.8});
alist.Add(new double[] { 7.9, 9.1, 1.3 });
alist.Add(new double[] { 8.6, 6.2, 5.7 });

double[][] jagged = new double[alist.Count][];

for (int i = 0; i < alist.Count; i++)
{
    double[] a = (double[])alist[i];

    jagged[i] = a;
}

//test - show the array
for (int i = 0; i < jagged.Length; i++)
{
    for (int n = 0; n < jagged[i].Length; n++)
    {
        MessageBox.Show(jagged[i][n].ToString());
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900