Converting a Multidimentional Array

A

AMP

Hello,
I am trying to convert MD array of ints to doubles;
I just want each x to be converted.
I am getting a Nullexception.
I was able to do this with a single dimention, but I am having
problems here.
OurData is passed correctly but GraphData looks like one dimention
when I get to Line commented with an error

public double[][] GraphData;
public Form3(int[][] OurData)
{
GraphData = new double[OurData.Length][];
for (int y=1;y<OurData.Length;y++)
{
foreach (int x in OurData[y])
{
GraphData[y][x] = System.Convert.ToDouble(x); //
ERROR
}

InitializeComponent();
}
}


Thanks for your help
Mike
 
F

Family Tree Mike

Notice that your inner loop is using the value of the array as the index into
it. I believe your inner loop should be a more traditional loop, as in for
(int idx = 0; idx < ourdata [y].length; ++idx). Also, your outer loop
"probably" is supposed to start at zero, but that is your choice.
 
N

NvrBst

Notice that your inner loop is using the value of the array as the index into
it.  I believe your inner loop should be a more traditional loop, as in for
(int idx = 0; idx < ourdata [y].length; ++idx).  Also, your outer loop
"probably" is supposed to start at zero, but that is your choice.



AMP said:
Hello,
I am trying to convert MD array of ints to doubles;
I just want each x to be converted.
I am getting a Nullexception.
I was able to do this with a single dimention, but I am having
problems here.
OurData is passed correctly but GraphData looks like one dimention
when I get to Line commented with an error
public double[][] GraphData;
        public Form3(int[][] OurData)
        {
            GraphData = new double[OurData.Length][];
            for (int y=1;y<OurData.Length;y++)
            {
            foreach (int x in OurData[y])
            {
                GraphData[y][x] = System.Convert.ToDouble(x);    //
ERROR
            }
            InitializeComponent();
            }
        }
Thanks for your help
Mike- Hide quoted text -

- Show quoted text -

FTM is correct, its usally, conceptually, easier to use traditional
loops, in this case. The reasion your code gives a null error
reference is because you never allocate the inner "double[]" (will
need two "new" keywords in your code for a double[][]). To conver a
int[][] to double[][] you can do something like the following.

-------------------
int[][] OurData = { new int[] { 5, 3, 1 }, new int[]
{ 3 }, new int[] { 1, 2, 3, 4, 5 } };
double[][] GraphData = new double[OurData.Length][];

for(int i = 0; i < OurData.Length; i++) {
GraphData = new double[OurData.Length];
for(int ii = 0; ii < OurData.Length; ii++)
GraphData[ii] = (double)OurData[ii];
}
-------------------

Alternativly, if you like foreach loops, you can do it the same way
you would a while loop (with manual counters - UntestedCode);
-------------------
int oCounter = 0, iCounter = 0;
foreach(int[] A in GraphData) {
GraphData[oCounter++] = new
double[OurData[oCounter]].Length];
foreach(int B in A) GraphData[oCounter][iCounter++] =
(double)B;
iCounter = 0;
}
 

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