在另一个阵列中存储多个阵列
问题描述:
我目前使用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
因此我可以检索这些值并计算每个学生的总值。
我不知道会有多少问题。
我也不知道会有多少学生。
稍后我可以循环使用这些值,但现在它是一个固定值。
我应该将值存储在字典,列表或元组中吗?
此后,我该如何调用方法,以便返回值并显示在“View”中?
我不需要将值放在表中,如果可能的话,一个简单的原始输出来检查算法的想法。
答
由于净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);
}
答
你可以使用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);
你可以看看结果here。
+0
感谢您的回复!如果我知道学生的数量和测试数量,我必须通过 – MaryLim
也许一个名字和元组的字典? – 2017-10-12 11:30:17
就像'Dictionary>'? –
David