C# 自动初始化不同长度的交错数组

C# 自动初始化不同长度的交错数组,c#,constructor,jagged-arrays,C#,Constructor,Jagged Arrays,我试图在类的构造函数中将锯齿数组的元素值初始化为0。虽然我能够指定其他变量的值,但对这个锯齿状数组这样做让我感到困惑 问题是交错数组的维数取决于另一个数组的值和属性。这就是我所做的,之后就是我试图获得的结果 public class PattRec { public int[] Userspecified; private double[][][] W = new double[][][] {}; public PattRec() { // He

我试图在类的构造函数中将锯齿数组的元素值初始化为0。虽然我能够指定其他变量的值,但对这个锯齿状数组这样做让我感到困惑

问题是交错数组的维数取决于另一个数组的值和属性。这就是我所做的,之后就是我试图获得的结果

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;
                }
            }    
        }
    }
}
如果“手动”指定,W应具有以下尺寸:

W[0]=new double[ ][ ];
W[1]=new double[5][3];
W[2]=new double[1][5];
显然,我做错了什么,因为它将数组抛出了边界异常。我试着用了很多其他的问题

也许从一开始我就做错了


向您致意,

我认为您没有正确地遍历数组。可以将其视为二维阵列的锯齿阵列。试着做些类似的事情,改编自

foreach(W中的双[,]数组)
{
for(int i=0;i
感谢@tnw的回答、见解和评论。这里的代码看起来像是我在工作的基础上完成的,对我来说很有用

但我无法解决一个问题:
我必须暗示,“神经元”将包含3个元素,99%的时间正确,但不灵活

如果有人知道如何解决这个问题

无论如何,代码如下:

public class PattRec
{
    public int[] neurone;
    private double[][][] W = new double[3][][];

    public PattRec()
    {
        neurone = new int[] { 3, 5, 1 };

        W[0] = new double[][] {new double[1],new double[neurone[1]]};
        W[1] = new double[][] {new double[neurone[1]],new double[neurone[0]] };
        W[2] = new double[][] {new double[neurone[2]],new double [neurone[1]] };

        foreach (double[][] array in W)
        {
            foreach (double[] item in array)
            {
                for (int i = 0; i < item.GetLength(0); i++)
                {
                    item[i] = 0;
                }    
            }

        }
    }

  // Rest of the class, some other methods here, etc.
}
公共类模式
{
公共int[]神经元;
私人双人房[][]W=新双人房[3][];
公共政策委员会(
{
神经元=新的int[]{3,5,1};
W[0]=new-double[][{new-double[1],new-double[neurone[1]]};
W[1]=新双[][{新双[神经元[1]],新双[神经元[0]]};
W[2]=新双[][{新双[神经元[2]],新双[神经元[1]]};
foreach(W中的双[]阵列)
{
foreach(数组中的双[]项)
{
for(int i=0;i
您可以使用递归方法。但是,它将要求您指定数组中每个元素的神经元大小(以某种方式)和初始值。神经元的大小将是内部数组的大小

this.InitializeJaggedArray(w, 5, 3);
....
private double[][][] w = new double[3][][];
private void InitializeJaggedArray(IList arr, object initializeValue, int neuronSize)
{
    for (int i = 0; i < arr.Count; i++)
    {
        if (arr[i] == null)
        {
            arr[i] = Activator.CreateInstance(arr.GetType().GetElementType(), new object[] { neuronSize } );
        }

        if(arr[i] is IList)
        {
            InitializeJaggedArray(arr[i] as IList, initializeValue, neuronSize);
        }
        else
        {
            arr[i] = initializeValue;
        }
    }
}
this.InitializeJaggedArray(w,5,3);
....
私人双人房[][]w=新双人房[3][];
私有void InitializeJaggedArray(IList arr、对象initializeValue、int-size)
{
对于(int i=0;i

这将初始化您提供给它的任何数组。

对于熟悉该主题的人,我正在尝试初始化隐层感知器(神经网络)的权重矩阵……据我所知,这可以完成任务,但前提是W中的数组已经初始化到某个维度。事实上,我认为这是我真正的问题,我首先不能简单地用正确的维度来创建它们…@Doombot啊,我看到了问题所在。我至少可以解释一下为什么你会遇到越界异常。您正在尝试遍历一个空数组。如果不另行指定,数组将初始化为长度0。我有答案,需要再等8个小时才能回答我自己的帖子,因为我没有rep;)谢谢你帮了我大忙@杜博很乐意帮忙。仅供参考,您也可以使用列表而不是数组。列表不需要有预定义的长度,并且会动态增长,因为您可以简单地向其中添加项目,而不是定义长度并将项目放置在特定索引中。通常比使用数组更容易,尽管它们都有各自的用例。进一步阅读:嗯,我需要计算数字,执行数十亿个数组的乘法和除法,我想我更擅长数组?
public class PattRec
{
    public int[] neurone;
    private double[][][] W = new double[3][][];

    public PattRec()
    {
        neurone = new int[] { 3, 5, 1 };

        W[0] = new double[][] {new double[1],new double[neurone[1]]};
        W[1] = new double[][] {new double[neurone[1]],new double[neurone[0]] };
        W[2] = new double[][] {new double[neurone[2]],new double [neurone[1]] };

        foreach (double[][] array in W)
        {
            foreach (double[] item in array)
            {
                for (int i = 0; i < item.GetLength(0); i++)
                {
                    item[i] = 0;
                }    
            }

        }
    }

  // Rest of the class, some other methods here, etc.
}
this.InitializeJaggedArray(w, 5, 3);
....
private double[][][] w = new double[3][][];
private void InitializeJaggedArray(IList arr, object initializeValue, int neuronSize)
{
    for (int i = 0; i < arr.Count; i++)
    {
        if (arr[i] == null)
        {
            arr[i] = Activator.CreateInstance(arr.GetType().GetElementType(), new object[] { neuronSize } );
        }

        if(arr[i] is IList)
        {
            InitializeJaggedArray(arr[i] as IList, initializeValue, neuronSize);
        }
        else
        {
            arr[i] = initializeValue;
        }
    }
}