在C#中创建二维数组的数组 [英] Creating an array of two-dimensional arrays in C#

查看:240
本文介绍了在C#中创建二维数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想创建一个二维数组来存储坐标点.

I simply want to create an array of two dimensional arrays to store coordinate points.

所以我想要一个数组,其中的每个索引都返回一个二维数组,该数组将用作 x y .

So I want an array where each index returns a two dimensional array which I would use as x and y.

这是我尝试过的:

waypoints = new int[4][,]    {
        {{0},{6, 0}},
        {{1},{1, 1}},
        {{2},{1, 5}},
        {{3},{6, 5}}
    };

我意识到这可能看起来很愚蠢,但是我尝试在Google上查找它,但没有得到任何好的结果.

I realize this probably looks stupid, but I've tried looking it up on Google, and I have not gotten any good results.

出现错误:

错误CS0623:数组初始化程序只能在变量或字段初始值设定项.尝试使用新的表达式代替"

"error CS0623: Array initializers can only be used in a variable or field initializer. Try using a new expression instead "

我也尝试过这样做:

waypoints = new int[4][,] {
        new int[,] {6, 0},
        new int[,] {1, 1},
        new int[,] {1, 5},
        new int[,] {6, 5}
        };

这给出了一个错误:

错误CS0846:预期使用嵌套的数组初始化程序"

"error CS0846: A nested array initializer was expected"

推荐答案

在初始声明中还需要另一个花括号集 {} :

One more curly bracket set {} is required in the initial declaration:

var waypoints = new int[4][,]   {
    new int[,] {{6}, {0}},
    new int[,] {{1}, {1}},
    new int[,] {{1}, {5}},
    new int[,] {{6}, {5}}
};

这是因为对于这样的2D数组,该数组中的每个元素都被视为用于初始化的数组(尽管通常每个数组元素都使用它,例如 val [0,0] = 4;).

This is because for such to 2D array, each element in the array is considered as an array for the initialization (albeit it is typically used per element of the array such as val[0,0] = 4;).

编辑(在评论反馈后):

int [] [] (称为<锯齿形数组)形成对比,即:数组成员的数组成员可以为固定尺寸​​.两者都是存储数组的数组,因此数组的每个元素都是一个数组.这解释了为什么需要在上面的初始化中放入另一个花括号.

Put in contrast with int[][] (known as jagged array, that is: array of arrays whose array member can be of different size), int[,] is a 2D array with fixed dimension. Both are array which stores arrays, and therefore each element of the array is an array. This explains why there is a need to put another curly bracket in its initialization as above.

这种2D数组,如果以不同的方式初始化,将导致尺寸不同(因此,有多种方法可以对其进行初始化):

Such 2D array, when initialized differently, will result in different dimension (and thus there are multiple ways to initialize it):

int[,] val = new int[,] { { 6 }, { 0 } }; //resulting in int[2,1]
int[,] val = new int[,] { { 6, 0 } }; //resulting in int[1,2]

无论哪种方式,都需要另外一组大括号.

In either way, additional set of curly bracket is needed.

要了解锯齿状数组与多维,固定大小的数组之间的区别,请已经 大量 可用 在线(来自知名来源).而且我知道,除了OP的兴趣之外,我可以提供更多有关它的信息并不重要.(因此,此答案的范围最初仅用于回答失败的初始化.)

For the differences between jagged array and multidimensional, fixed sized, array, there are already plenty good explanations/benchmarking available online from well reputed sources. And I understand that it wouldn't be significant, apart from the OP's interest, for me to put more info about it. (And thus the scope for this answer is originally directed only to answer the failed initialization).

可以理解,该解决方案不是最好用于存储坐标点(如OP所做的那样).上面的解释是为了解释为什么他的初始化不起作用,而不是提供用于存储坐标点的最佳解决方案.至于存储坐标点,请 System.Drawing struct 中会更合适(如注释中所建议).

It is understood that the solution is not best used for storing coordinate points (as done by OP). The explanation above is given to explain why his initialization doesn't work, rather than to provide best solution for storing coordinate points. As for storing coordinate points, Point in the System.Drawing struct will be more proper (as suggested in the comment).

使用2D数组表示2D中的单点笛卡尔坐标是一种过大的杀伤力,因为一维数组已经能够存储计算机允许的尽可能多的数字,所以在直角坐标系中存储点所需的数字远远超过了两个.

The use of 2D array to represent single point in 2D Cartesian coordinate is an "overkill", as a 1D array is already capable of storing as many numbers as the computer allows, much more than two numbers required to store points in Cartesian coordinate system.

这篇关于在C#中创建二维数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆