如何删除字典中的值 [英] How can I remove a value in a dictionary

查看:179
本文介绍了如何删除字典中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在我的代码中使用了字典

字典< Int32,字典< Int32,列表< Int32>>> ; Staffs =  new  Dictionary< int,Dictionary< int,List< int>>>(); 



然后添加密钥

  if (!Staffs.Keys.Contains(staff))
{
Staffs.Add(staff, new Dictionary< int,List< int>>());
}

if (!Staffs [staff] .Keys.Contains(triad))
{
员工[staff] .Add(triad, new List< int>());
}



再添加一些价值

...

  if (staff ==  1 && triad ==  1 
{
triad1 =员工[ 1 ] [ 1 ] ToArray的();的Array.Sort(triad1);
mypoint.Add(newpoint);

}



...

现在我想从员工中删除一些价值[1] [1]

???????????????????????????????????????? ???????????

请...



我尝试过:



我测试员工[1] [1] .RemoveAt(value);但它不起作用

解决方案

字典< Int32,字典< Int32,List< Int32>>> Staffs =  new  Dictionary< int,Dictionary< int,List< int>>>(); 

Dictionary< Int32,List< Int32>> staffsDataItem = new Dictionary< int,List< int>>();
staffsDataItem.Add( 10 new 列表< int> { 11 12 13 });
Staffs.Add( 1 ,staffsDataItem);

staffsDataItem = new Dictionary< int,List< int>>();
staffsDataItem.Add( 20 new List< int> { 21 22 23 });
staffsDataItem.Add( 200 new 列表< int> { 201 202 203 });
Staffs.Add( 2 ,staffsDataItem);

staffsDataItem = new Dictionary< int,List< int>>();
staffsDataItem.Add( 30 new 列表< int> { 31 32 33 });
Staffs.Add( 3 ,staffsDataItem);

// 删除整个员工3项
Staffs.Remove( 3 );

// 从员工2项中删除20项

员工[ 2 ]。删除( 20 );
Console.ReadLine();
}


使用字典(TKey,TValue).Remove方法(TKey)(System.Collections.Generic) [ ^ ]

在你的情况下,那将是:

员工[ 1 ]。删除( 1  ); 


Hi
I used a dictionary in my code

Dictionary<Int32, Dictionary<Int32, List<Int32>>> Staffs = new  Dictionary<int, Dictionary<int, List<int>>>();


then add keys

if (!Staffs.Keys.Contains(staff))
               {
                   Staffs.Add(staff, new Dictionary<int, List<int>>());
               }

               if (!Staffs[staff].Keys.Contains(triad))
               {
                   Staffs[staff].Add(triad, new List<int>());
               }


and then add some value
...

if (staff == 1 && triad == 1)
                   {
                       triad1 = Staffs[1][1].ToArray(); Array.Sort(triad1);
                       mypoint.Add(newpoint);

                   }


...
NOW I want to remove some value from staff[1][1]
????????????????????????????????????????????????????????
please...

What I have tried:

I test Staffs[1][1].RemoveAt(value); but It didn't work

解决方案

Dictionary<Int32, Dictionary<Int32, List<Int32>>> Staffs = new Dictionary<int, Dictionary<int, List<int>>>();

Dictionary<Int32, List<Int32>> staffsDataItem = new Dictionary<int, List<int>>();
staffsDataItem.Add(10, new List<int> {11, 12, 13});
Staffs.Add(1, staffsDataItem);

staffsDataItem = new Dictionary<int, List<int>>();
staffsDataItem.Add(20, new List<int> { 21, 22, 23 });
staffsDataItem.Add(200, new List<int> { 201, 202, 203 });
Staffs.Add(2, staffsDataItem);

staffsDataItem = new Dictionary<int, List<int>>();
staffsDataItem.Add(30, new List<int> { 31, 32, 33 });
Staffs.Add(3, staffsDataItem);

// delete the entire Staffs "3" item
Staffs.Remove(3);

// delete the "20" item from the Staffs "2" item

Staffs[2].Remove(20);
    Console.ReadLine();
}


Use the Dictionary(TKey, TValue).Remove Method (TKey) (System.Collections.Generic)[^]
In your case, that would be:

Staffs[1].Remove(1);


这篇关于如何删除字典中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆