在 C# 中,有人可以帮我将多个字符串数组分配给二维字符串数组吗?
这是我的代码:
string[] test1 = new string[5] { "one", "two", "three", "four", "five" };
string[] test2 = new string[5] { "one", "two", "three", "four", "five" };
string[] test3 = new string[5] { "one", "two", "three", "four", "five" };
string[] test4 = new string[5] { "one", "two", "three", "four", "five" };
string[,] allTestStrings = new string [4, 5];
allTestStrings[0] = test1;
allTestStrings[1] = test2;
allTestStrings[2] = test3;
allTestStrings[3] = test4;
对于每个 2d 作业,我收到以下错误:
[] 内的索引数量错误;预计 2
我在上面的代码中做错了什么?
提前致谢。
您必须为二维数组指定两个索引,例如
allTestStrings[0, 0] = test1[0];
allTestStrings[0, 1] = test1[1];
您可以提取一个方法来循环执行此操作:
for (var i = 0; i < test1.Length; i++)
{
allTestStrings[0, i] = test1[i];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)