當前位置:成語大全網 - 新華字典 - 如何在dictionary遍歷時刪除對應元素

如何在dictionary遍歷時刪除對應元素

思路

因在循環中不能移除dictonary的值,故定義壹個集合存放需要移除的key

循環dictonary,拿到對應的key放入集合裏

循環放key的集合,移除dictonary的值

?Dictionary<String,?String>?testd?=?new?Dictionary<string,?string>();

List<String>?list?=?new?List<String>();

foreach?(var?item?in?testd)

{

if?(item.Key.Equals("*"))?{

list.Add(item.Key);

}

}

foreach?(String?str?in?list)?{

testd.Remove(str);

}