[英]Storing more than one array in another array
我目前正在为我的应用程序使用ASP.NET Core MVC ,但不确定如何解决该问题。
说我有两个双精度数组
double[] questionOne = {1,4,5,2,4};
double[] questionTwo = {3,2,4,5,2};
我想使用一种方法将它们连接在一起,并将它们存储在可能的字典中,这样存储的值就像
stud1 | 1,3
stud2 | 4,2
stud3 | 5,4
stud4 | 2,5
stud5 | 4,2
这样我就可以检索值并计算每个学生的总价值。
我不知道会有多少个问题。
我也不知道会有多少学生。
稍后,我将可以为这些值循环,但是现在,它是一个固定值。
我应该将值存储在字典,列表还是元组中?
此后,如何调用该方法,以便可以返回值并显示在“视图”中?
我不需要将值放在表中,如果可能的话,可以通过简单的原始输出来检查算法思想。
您可以使用LINQ:
List<Tuple<double, double>> tuples =
questionOne.Zip(questionTwo, (one, two) => Tuple.Create(one, two)).ToList();
合并数字数组。 您可以为学生做同样的事情:
string[] students = new string[] {"stud1", "stud2", "stud3", "stud4", "stud5"};
Dictionary<string, Tuple<double, double>> result = students
.Zip(tuples, (student, tuple) => new { student, tuple })
.ToDictionary(entry => entry.student, entry => entry.tuple);
您可以在此处查看结果。
从.Net 4.7开始,您可以使用以下代码:
using System;
using System.Linq;
public class Program
{
public static void Main()
{
double[] questionOne = {1, 4, 5, 2, 4};
double[] questionTwo = {3, 2, 4, 5, 2};
var combined = questionOne.Zip(questionTwo, (q1, q2) => (q1, q2)).ToList();
Console.WriteLine(combined);
}
}
您可以使用以下结构:
Dictionary<string, string[]> myDictionary= new Dictionary<string, string[]>();
那么您只需要一个添加内容的算法,例如:
for(int i=0; i<array1.Length; i++) {
String[] data = new String[2];
data[0] = array1[i];
data[1] = array1[i];
myDictionary.Add("student"+i, data);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.