Converting a Multidimentional Array

  • Thread starter Thread starter AMP
  • Start date Start date
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
 
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.
 
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;
}
 
Back
Top