有人可以帮我将多个字符串数组分配给一个2d字符串数组吗? [英] Can someone help me assign multiple string arrays into one 2d string array?

查看:64
本文介绍了有人可以帮我将多个字符串数组分配给一个2d字符串数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,有人可以帮助我将多个字符串数组分配给2d字符串数组吗?

In C#, can someone help me assign multiple string arrays to a 2d string array?

这是我的代码:

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作业都遇到以下错误:

I am getting the following error for each 2d assignment:

[]中的索引数量错误;预期2

Wrong number of indices inside []; expected 2

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

What am I doing wrong in the above code?

谢谢.

推荐答案

您必须为2D数组指定两个索引,例如

You have to specify both indicies for your 2D array, e.g.

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

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

You could extract a method to do this in a loop:

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

这篇关于有人可以帮我将多个字符串数组分配给一个2d字符串数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆