简体   繁体   English

如何声明2维的锯齿状数组?

[英]How do I declare a Jagged array of 2 dimensions?

I m trying to declare a 2D dynamic array below is my code: 我想在下面声明一个2D动态数组是我的代码:

   var marray= new[,]
                        {
                            {
                                "1", 
                                "Module 1.1",
                                "Module 1.2",
                                " Module 1.3",
                                "Module 1.4",
                                "Module 1.5"
                            },

                            {
                                "2", 
                                "Module 2.1"                                    
                            }
                        };

I am getting error on second value "An array initializer of '6' is expected" . 我在第二个值“预期为'6'的数组初始化器”上出错。 I can understant it is expecting 2nd to be having 6 values but I need it to be dynamic of any length. 我可以确定它期望2nd具有6个值,但是我需要它可以是任意长度的动态值。 I dont know much about array so cannot resolve it. 我对数组了解不多,所以无法解决它。 Can you please guide. 可以请您指导。 Thanks 谢谢

You want a ragged array rather than a straight multi-dimensional array because your sub-array sizes are not the same. 您需要一个粗糙的数组,而不是一个的多维数组,因为您的子数组大小不同。

Declare as 声明为

var marray= new[][]

rather than 而不是

var marray= new[,]

Refs: 参考:

You're receiving the compile error because your sub-array sizes are not equivalent. 您收到编译错误,因为您的子数组大小不相等。 You'll either need to implement this using jagged arrays as Mitch Wheat suggests or ensure that all elements in the sub-array have the same number of elements. 您可能需要按照Mitch Wheat的建议使用锯齿状数组来实现此目的, 或者确保子数组中的所有元素都具有相同数量的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM