C# 有人能帮我将多个字符串数组分配到一个2d字符串数组中吗?
在C#中,有人能帮我将多个字符串数组分配给一个2d字符串数组吗 这是我的密码:C# 有人能帮我将多个字符串数组分配到一个2d字符串数组中吗?,c#,arrays,string,multidimensional-array,variable-assignment,C#,Arrays,String,Multidimensional Array,Variable Assignment,在C#中,有人能帮我将多个字符串数组分配给一个2d字符串数组吗 这是我的密码: 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"
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
在上面的代码中我做错了什么
提前感谢。您必须为2D阵列指定两个标记,例如
allTestStrings[0, 0] = test1[0];
allTestStrings[0, 1] = test1[1];
您可以提取一个方法在循环中执行此操作:
for (var i = 0; i < test1.Length; i++)
{
allTestStrings[0, i] = test1[i];
}
for(var i=0;i
您可以这样初始化它:
string[,] arr = {
{ "one", "two", "three", "four", "five" },
{ "one", "two", "three", "four", "five" },
{ "one", "two", "three", "four", "five" },
};
您可以使用锯齿阵列,如下所示:
string[][] allTestStrings = new string[4][];
allTestStrings[0] = test1;
allTestStrings[1] = test2;
allTestStrings[2] = test3;
allTestStrings[3] = test4;
多维数组([,]
)不是锯齿状数组([],[]
)。