我已经定义了一个字典,它的值本身就是一个字典,该内部字典的值是字符串列表,如下所示:
Dictionary<string, Dictionary<string, List<string>>> myDictionary;
这个结构对我来说很复杂,现在还不知道如何为它添加值并检查键是否退出等等。 所以我从:
开始 Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
if (!myDictionary.ContainsKey("outerKey"))
{
myDictionary.Add("someOuterKey", ???);
}
然后我卡住了。你可以帮忙写这个吗?
答案 0 :(得分:3)
以下是如何向outerkey / innerkey结构添加值的示例:
var outerKey = "Some Outer Key";
var innerKey = "Some Inner Key";
var myValue = "Some Value";
if (!myDictionary.ContainsKey(outerKey))
{
myDictionary.Add(outerKey, new Dictionary<string, List<string>>());
}
if (!myDictionary[outerKey].ContainsKey(innerKey))
{
myDictionary[outerKey].Add(innerKey, new List<string>());
}
myDictionary[outerKey][innerKey].Add(myValue);
答案 1 :(得分:3)
这很抽象,可能有助于减少抽象,然后尝试理解它。
考虑外部目录可以是&#34; Family&#34;,内部目录可以是&#34;会员&#34;和列表可以是&#34;朋友&#34;,然后执行以下操作是有意义的:
var families = new Dictionary<string, Dictionary<string, List<string>>>();
var members = new Dictionary<string, List<string>>();
var friends = new List<string> { "Fred", "Arnold", "Rebecca" };
members.Add("John", friends);
families.Add("Smith", members);
我总是发现上下文有助于解决这些问题。只需将变量的名称更改为项目中有意义的名称即可。
标记
答案 2 :(得分:1)
这对你有帮助吗?
Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
if (!myDictionary.ContainsKey("outerKey"))
{
myDictionary.Add("outerKey", new Dictionary<string, List<string>>());
}
初始化后,以下是如何为其添加值
public void Add(string outerKey, string innerKey, string innerValue)
{
//add to out dictionary
if (!myDictionary.ContainsKey("outerKey"))
{
myDictionary.Add("outerKey", new Dictionary<string, List<string>>());
}
//add to inner dictionary
if(!myDictionary[outerKey].ContainsKey(innerKey))
{
myDictionary[outerKey].Add(innerKey,new List<string>());
}
// add to list of inner dictionary
myDictionary[outerKey][innerKey].Add(innerValue);
}
检查是否添加了键,否则fetcch将返回null
public List<string> FetchValue(string outerKey, string innerKey)
{
List<string> result = null;
if(myDictionary.ContainsKey(outerKey))
result = myDictionary[outerKey][innerKey];
return result;
}
答案 3 :(得分:1)
答案取决于你想要达到的目标。甚至可能存在更容易使用的数据结构。但是,我们假设您要为loweststringa
添加loweststringb
和innerstring1
outerstring
(复杂的ooohh)。
然后就可以了:
List<string> lowestList = new List<string>();
lowestList.AddRange(new[]{"loweststringa", "loweststringb"});
Dictionary<string, List<string>> innerDictionary = new Dictionary<string, List<string>>();
innerDictionary.Add("innerstring1", lowestList);
Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
myDictionary.Add("outerstring", innerDictionary);
您添加到myDictionary
的密钥属于string
类型。 myDictionary
的值是Dictionary<string, List<string>>
的实例。
这些内部词典的键当然也是string
类型,值类型为List<string>
。
如果你想检查&#34; loweststringb&#34;存在于&#34; innerstring1&#34;的列表中在&#34; outerstring&#34;的词典中你可以做到以下几点:
if (myDictionary.ContainsKey("outerstring"))
if (myDictionary["outerstring"].ContainsKey("innerstring1"))
if (myDictionary["outerstring"]["innerstring1"].Contains("loweststringb"))
// hooray it's there!
但请确保您不要在此构造中插入null
引用作为词典或列表。上面的例子并不是最有效的,因为它遍历最外面的字典三次。
希望这会对你有所帮助。
更新:插入&#34; loweststringc&#34;在现有清单中:
myDictionary["outerstring"]["innerstring1"].Add("loweststringc");
答案 4 :(得分:1)
希望这能解决您的问题。
Dictionary<string, Dictionary<string, List<string>>> myDictionary = new Dictionary<string, Dictionary<string, List<string>>>();
if (!myDictionary.ContainsKey("outerKey"))
{
var innerDictiionary= new Dictionary<string, List<string>>();
var data= new List<sting>(){
"hello"
};
innerDictiionary.Add("innerKey", data);
myDictionary.Add("outerKey", innerDictiionary);
}