动态大小C#字典列表 [英] Dynamic size C# Dictionary List
本文介绍了动态大小C#字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C#编程的新手,我需要一个帮助来创建一个Dictionary列表来存储一些字符串数据。看下面的例子
row1-> cat,dog,rat
row2-> 总线
row3-> 星期日,星期一,星期二,星期五
就像有不同列数的那样。
请告诉我如何创建字典对象和存储数据如上所述以及如何阅读它。谢谢。
Hi, i am new for programming in C# and i need a help to create a Dictionary list to store some string data. see the following example
row1-> "cat","dog","rat"
row2-> "bus"
row3-> "sunday","monday","tuesday","friday"
like that with different number of column.
please tell me how to create an object of dictionary and store data as above and how to read it also . thanks.
推荐答案
你的意思是什么?!
Do you mean something like that?!
var myList = new Dictionary<String, List<String>>();
myList.Add("row1", new List<String>());
myList.Add("row2", new List<String>());
myList.Add("row3", new List<String>());
myList["row1"].Add("cat");
myList["row1"].Add("dog");
myList["row1"].Add("rat");
myList["row2"].Add("bus");
myList["row3"].Add("sunday");
myList["row3"].Add("monday");
myList["row3"].Add("tuesday");
myList["row3"].Add("friday");
Console.WriteLine("Number of rows -> " + myList.Count);
foreach(var row in myList) {
Console.WriteLine("Number of columns in " + row.Key + " -> " + row.Value.Count);
}
充实解决方案[/ edit]
[edit]fleshing out solution[/edit]
你不需要字典
:只需使用List<列表<字符串> >
。例如
You don't need aDictionary
: just use aList < List < string > >
. e.g.
List<List<string>> m = new List<List<string>>();
m.Add(new List<string>());
m.Add(new List<string>());
m.Add(new List<string>());
m[0].AddRange( new string[] {"cat", "dog", "rat"});
m[1].Add("bus");
m[2].AddRange(new string[] { "sunday", "monday", "tuesday", "friday" });
foreach (var row in m)
{
foreach (var s in row)
Console.Write("{0} ", s);
Console.WriteLine();
}
这篇关于动态大小C#字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文