I am trying to initialize the values of the elements of a jagged array to 0 in the constructor of a class. While I am able to specify the values of other variables, doing so for this jagged array eludes me.
The problem is that the dimensions of the jagged array is dependant of the values and properties of another array. This is what I did, and after is the result I am trying to obtain.
public class PattRec
{
public int[] Userspecified;
private double[][][] W = new double[][][] {};
public PattRec()
{
// Here are the specs about the jagged array
Userspecified= new int[] { 3, 5, 1 };
// Here I try to add "Userspecified.Lenght" elements to
// the first dimension of the jagged array
for (int i = 0; i < Userspecified.Length; i++)
{
W[i] = new double[][]
{
new double[]{},
new double[]{},
};
}
// Here I try to set the values of the elements of "W" to 0
// The second and third dimension of the jagged arrays are
// linked to the values in "Userspecified"
for (int i = 1; i < Userspecified.Length; i++)
{
for (int a = 0; a < Userspecified[i]; a++)
{
for (int b = 0; b < Userspecified[i-1]; b++)
{
W[i][a][b] = 0;
}
}
}
}
}
In the end, given
Userspecified= new int[ ] { 3, 5, 1 };
If specified "by hand", W should have these dimensions:
W[0]=new double[ ][ ];
W[1]=new double[5][3];
W[2]=new double[1][5];
Obviously, I am doing something wrong since it throws me array out of bounds exceptions. I tried to use http://msdn.microsoft.com/en-us/library/2s05feca.aspx and other questions on SO.
Maybe I am doing this wrong since the beginning though...
Best regards,