c# - 有人可以帮我将多个字符串数组分配给一个二维字符串数组吗?

标签 c# arrays string multidimensional-array variable-assignment

在 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 作业,我都收到以下错误:

Wrong number of indices inside []; expected 2

上面的代码我做错了什么?

提前致谢。

最佳答案

您必须为二维数组指定两个索引,例如

allTestStrings[0, 0] = test1[0];
allTestStrings[0, 1] = test1[1];

您可以提取一个方法来在循环中执行此操作:

for (var i = 0; i < test1.Length; i++)
{
    allTestStrings[0, i] = test1[i];
}

关于c# - 有人可以帮我将多个字符串数组分配给一个二维字符串数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30435401/

相关文章:

c# - 检查日期是否在范围内的最简洁方法(可空日期)

c# - 为所有 future 项目永久添加库

c++ - 我是否应该始终声明比我的字符串大的 char 数组?

javascript - 在javascript中调用存储在数组中的多个参数的函数

regex - 用于在子字符串中用破折号替换空格的正则表达式。

string - 连接两个字符串,其中第一个字符串最后有一个空格 Matlab

c# - 线程卡在 MySqlCommand.ExecuteNonQuery() 中

C# ITypeInfo.GetContainingTypeLib 在传递 VBA 类的实时实例时失败

c++ - 在 C++ 中初始化数组

c - 在方法中传递字符数组作为参数,C 语言