如何将多个一维数组组合成一个多维数组,从而产生列出的显示
本文关键字:显示 数组 一个 组合 一维数组 | 更新日期: 2023-09-27 18:34:42
嗨,我试图让我的代码工作,但不幸的是,我在将我的数组组合成一个列表时遇到了一些麻烦。
我希望列出的内容如下所示:
1.a, 1.b, 1.c, 1.d,1.e, 2.a, 2.b, 2.c, 2.d, 2.e
每个调用数组 1 中的号码(即 1,2,3,4,5,6,...(和数组 2 中的问题(即 A,B,C,D,E(
但是,由于它被称为,我还想记录对每个数字和问题的回答。
1.A 响应 1, 1.B 响应 2, 1.C 响应 1...等等。
到目前为止,我有这段代码,但它只是告诉我:
const int phone_number = 50;
string[] phone_number1;
phone_number1 = new string[phone_number];
const int question = 5;
string[] question1;
question1 = new string[question];
const int answers = phone_number + question;
string[] answer1;
answer1 = new string[answers];
50 5 55
制作三个数组,伪代码:
string[] num;
string[] que;
string[] res;
将它们放在第四个数组中
Object[] acc = {num, que, res};
将其放入您正在维护的 ArrayList 中。
lsit.Add(acc);
这只是一个想法,您应该避免使用 Object 数组,如果允许,最好的方法是为这些属性创建一个类并将它们保存在列表中。
好吧,您想要的一部分(一个例子(...大概是这样的
int[] intarr = { 1, 2, 3, 4, 5, 6 };
char[] chrarr = { 'a', 'b', 'c', 'd', 'e' };
ArrayList alist = new ArrayList();
for (int j = 0; j < chrarr.Length; j++)
{
for (int i = 0; i < intarr.Length; i++)
{
alist.Add(intarr[i] + "." + chrarr[j]);
}
}
alist.Sort();
foreach (string str in alist)
{
Console.WriteLine(str);
}
输出:
1.a 1.b 1.c 1.d 2.a 2.b 2.c 2.d 3.a 3.b 3.c 3.d 4.a 4.b 4.c 4.d 5.a 5.b 5.c 5.d