[英]Assign values to 2D array
I need to assign values to 2D array but I can't find the right syntax. 我需要将值分配给2D数组,但找不到正确的语法。 I tried this but is't wrong: 我试过了,但是没有错:
string[][] s2d = new string[][] {
{"val1","val2"},
{"val1bis","val2bis"}
};
Thanks 谢谢
You are almost there, just change the convention
and use [,].
您快到了,只需更改convention
并使用[,].
, [][]
used to define Array of arrays ( Jagged
arrays). , [][]
用于定义数组Array( Jagged
数组)。
string[,] s2d = new string[,] {
{"val1","val2"},
{"val1bis","val2bis"}
};
If you want to enumerate on Multidimensional arrays you can do that but it is a flatten array. 如果要枚举多维数组,则可以这样做,但这是一个扁平数组。
foreach(var s in s2d)
{
// logic
}
Just access the elements in traditional way (if want). 只需以传统方式访问元素(如果需要)。
for(int i=0;i < s2d.GetLength(0);i++)
for(int j=0;j < s2d .GetLength(1);j++)
var val = s2d [i,j];
You're using an incorrect syntax: 您使用的语法不正确:
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Source: https://msdn.microsoft.com/it-it/library/2yd9wwz4.aspx 来源: https : //msdn.microsoft.com/it-it/library/2yd9wwz4.aspx
If you truly want a 2d array 如果您真的想要二维数组
string[,] s2d = new string[2,2] { {"val1","val2"}, {"val1bis","val2bis"}};
string[][] gives you a possibility of a jagged array string [] []使您有可能出现锯齿状数组
If you really want array of arrays so declare array of arrays 如果您真的想要数组数组,请声明数组数组
string[][] s2d = new string[][] {
new string[] {"val1","val2"},
new string[] {"val1bis","val2bis"}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.