删除在foreach {...} [英] RemoveAt in foreach { ... }

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

问题描述

是否有任何有效的方法在foreach {...}

例程中进行Dictionary<> .RemoveAt?

例如,这个例子是安全的还是在那里任何推荐的方式?


foreach(KeyValuePair< int,in mymap)

{

if(a.Value!= GoodValue)

{

mymap.RemoveAt(a.Key); //并继续迭代

}

}


请回复。在此先感谢。


Hyun-jik Bae

Is there any efficient way doing Dictionary<>.RemoveAt in foreach { ... }
routine?
For example, is this example safe or is there any recommended way?

foreach(KeyValuePair<int,inta in mymap)
{
if(a.Value!=GoodValue)
{
mymap.RemoveAt(a.Key); // and continues the iteration
}
}

Please reply. Thanks in advance.

Hyun-jik Bae

推荐答案



" ; Hyun-jik Bae < im *********** @ paran.comwrote in message

news:Ok ************** @ TK2MSFTNGP02.phx。 gbl ...

"Hyun-jik Bae" <im***********@paran.comwrote in message
news:Ok**************@TK2MSFTNGP02.phx.gbl...

在foreach {...}

例程中是否有任何有效的方法进行字典<> .RemoveAt?

例如,这个例子是安全的还是有推荐的方法?


foreach(KeyValuePair< int,in mymap)

{

if(a.Value!= GoodValue)

{

mymap.RemoveAt(a.Key); //并继续迭代

}

}
Is there any efficient way doing Dictionary<>.RemoveAt in foreach { ... }
routine?
For example, is this example safe or is there any recommended way?

foreach(KeyValuePair<int,inta in mymap)
{
if(a.Value!=GoodValue)
{
mymap.RemoveAt(a.Key); // and continues the iteration
}
}



我不记得确切但我是阅读关于这个的事情

天我相信它不安全但有一个很好的方法可以解决它。


我不确定是否尽管如此,它仍然可以在临时副本上工作。


即,您可以通过创建mymap的浅层副本轻松绕过它,然后继续工作内循环中的那个温度。

对不起,但这是我能做到的最好的事情(我虽然我只是提到它而已经提到它了

没有得到很多回复)。


Jon

I don''t remember exactly but I was reading something about this the other
day and I believe its not safe but there is a good way to fix it.

I''m not sure if it involes working on a temp copy or not though.

i.e., you could easily get around it by creating a shallow copy of mymap and
then working on that temp in the inner loop.
Sorry but thats the best I could do(I though I''d mention it just incase you
don''t get to many replies).

Jon


你不能改变你正在循环的集合。更改

该集合使枚举器无效,当您尝试继续循环时它会抛出

异常。


一种方法是将要删除的键添加到列表中,

然后当您循环浏览字典时,将循环遍历

列表从字典中删除项目。


Hyun-jik Bae写道:
You can''t change the collection that you are looping through. Changing
the collection makes the enumerator invalid, and it will throw an
exception when you try to continue the loop.

One way to do it is to add the keys that you want to remove to a list,
then when you have looped through the dictionary, you loop throught the
list to remove the items from the dictionary.

Hyun-jik Bae wrote:

有没有有效的方式做词典<> ;。删除在foreach {...}

例程?

例如,这个示例是安全的还是有任何推荐的方式?


foreach(KeyValuePair< int,in mymap)

{

if(a.Value!= GoodValue)

{

mymap.RemoveAt(a.Key); //并继续迭代

}

}


请回复。在此先感谢。


Hyun-jik Bae
Is there any efficient way doing Dictionary<>.RemoveAt in foreach { ... }
routine?
For example, is this example safe or is there any recommended way?

foreach(KeyValuePair<int,inta in mymap)
{
if(a.Value!=GoodValue)
{
mymap.RemoveAt(a.Key); // and continues the iteration
}
}

Please reply. Thanks in advance.

Hyun-jik Bae


Hyun-jik Bae,


您使用的是哪个系列?字典没有RemoveAt

方法。


正如其他人所说删除项目会使调查员无效。

它'最好弄清楚你想要在一个循环中删除什么然后

实际上在另一个循环中移除。


Brian


Hyun-jik Bae写道:
Hyun-jik Bae,

Which collection are you using? Dictionary does not have a RemoveAt
method.

As others have said removing an item will invalidate the enumerator.
It''s better to figure out what you want to remove in one loop and then
actually remove in another.

Brian

Hyun-jik Bae wrote:

是否有任何有效的方法在foreach {...}
$中进行字典<> .RemoveAt b $ b例程?

例如,这个例子是安全的还是有推荐的方法?


foreach(KeyValuePair< int,in mymap)<如果(a.Value!= GoodValue)

{

mymap.RemoveAt(a.Key) ; //并继续迭代

}

}


请回复。在此先感谢。


Hyun-jik Bae
Is there any efficient way doing Dictionary<>.RemoveAt in foreach { ... }
routine?
For example, is this example safe or is there any recommended way?

foreach(KeyValuePair<int,inta in mymap)
{
if(a.Value!=GoodValue)
{
mymap.RemoveAt(a.Key); // and continues the iteration
}
}

Please reply. Thanks in advance.

Hyun-jik Bae


这篇关于删除在foreach {...}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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