我想使用一个二维数组,但是我事先不知道它的大小。所以我的问题是:如何声明它?然后如何向其添加值?
String[,] tabConfig = new String[?, 4];
foreach(blabla with i)
{
tabConfig[i, 0] = a;
tabConfig[i, 1] = b;
tabConfig[i, 2] = c;
tabConfig[i, 3] = d;
}
我知道我也可以使用列表,但我对它不是很熟悉。
谢谢!
编辑:准备好了!这是我真正的代码,得到了Jon Skeet的帮助!
List<string[]> tabConfig = new List<string[]>();
String[] temp = new String[4];//The array that will be inside the List
int line = 0, column = 0;
foreach (XmlNode e in doc.DocumentElement.ChildNodes)
{
if (e.Attributes["Server"].Value == choice)
{
temp[0] = e.Attributes["Serveur"].Value;//Here is value 'a'
column = 1;
foreach (XmlNode i in e.ChildNodes)
{
temp[colonne] = i.InnerText;//Here are values 'b', 'c' and 'd'
column++;
}
tabConfig.Add(temp);//Put a new line into the List
line++;
}
}
然后进行调用:
foreach(string[] array in tabConfig)
foreach(String txt in array)
Console.WriteLine(txt);
List<List<string>>
。 - Matt Burland