[英]Dictionary in C# with list of Int[] values not displaying
I am trying to display the values from a dictionary function but it does not display the value, what it shows is below - 我正在尝试显示来自字典函数的值,但是它不显示该值,它显示的内容如下-
1007 System.Collections.Generic.List`1[System.Int32[]]
1006 System.Collections.Generic.List`1[System.Int32[]]
1009 System.Collections.Generic.List`1[System.Int32[]]
1008 System.Collections.Generic.List`1[System.Int32[]]
the code I am using is shown below 我正在使用的代码如下所示
clsCollaborativeFilter mri = new clsCollaborativeFilter();
Dictionary<int, List<int[]>> movRecommendations = mri.aList1();
foreach (KeyValuePair<int, List<int[]>> kvp in movRecommendations)
{
da.Text += kvp.Key;
da.Text += " ";
da.Text += kvp.Value;
da.Text += "<br/>";
}
return da.Text;
I cant seem to understand why this is happening 我似乎无法理解为什么会这样
you are adding a List
of an Array
of ints
to a string, in the line: da.Text += kvp.Value;
您将在一行
da.Text += kvp.Value;
ints
Array
List
添加到字符串中: da.Text += kvp.Value;
so what it is doing, is it is adding the signature of the List
of Array
s because it doesn't know exactly what you are trying to do. 所以它在做什么,是要添加
Array
的List
的签名 ,因为它不完全知道您要做什么。
What you can do instead is: 您可以做的是:
da.Text += String.Join(", ", kvp.Value.SelectMany(i => i));
...or assuming that it really is a dictionary-of-list-of-int-arrays, then: ...或者假设它确实是整数数组列表字典,则:
StringBuilder sb = new StringBuilder();
foreach(Int32 key in movRecommendations.Key) {
List<Int32[]> listOfArrays = movRecommendations[ key ];
sb.Append( key );
sb.AppendLine();
foreach(Int32[] array in listOfArrays) {
Boolean isFirst = true;
foreach(Int32 val in array) {
if( !isFirst ) sb.Append( ", " );
sb.Append( val );
isFirst = false;
}
sb.AppendLine();
}
sb.AppendLine("<br />");
}
da.Text = sb.ToString();
I think what you actually want is this: 我认为您真正想要的是:
Dictionary< int, List<int> >
...a List<int[]>
doesn't make much sense to me. ...一个
List<int[]>
对我来说没有多大意义。
clsCollaborativeFilter mri = new clsCollaborativeFilter();
Dictionary<int, List<int[]>> movRecommendations = mri.aList1();
foreach (KeyValuePair<int, List<int[]>> kvp in movRecommendations)
{
da.Text += kvp.Key;
da.Text += " ";
da.Text += string.Join(",",kvp.Value);
da.Text += "<br/>";
}
return da.Text;
Dictionary<int, List<int[]>> movRecommendations = new Dictionary<int, List<int[]>>(){
{0, new List<int[]>(){ new int[]{1, 2, 3} }},
{1, new List<int[]>(){ new int[]{7, 8, 9} }}
};
string da = String.Empty;
foreach (KeyValuePair<int, List<int[]>> kvp in movRecommendations)
{
da += kvp.Key;
da += " ";
da += String.Join(", ", kvp.Value.SelectMany(i => i));
da += "<br/>";
}
Console.WriteLine (da);
This will write out 0 1, 2, 3<br/>1 7, 8, 9<br/>
这将写出
0 1, 2, 3<br/>1 7, 8, 9<br/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.