ForEach&删除问题 [英] ForEach & Remove Problem

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

问题描述

这里有一些描述我正在尝试做什么的伪代码:


foreach(收集对象)

{

如果(某些条件)

collection.Remove(object);

}


这个问题是,如果从集合中删除了任何东西,那么foreach就会搞砸了。处理这种情况的最佳方法是什么?


-

罗伯特W.

温哥华,不列颠哥伦比亚省
www.mwtech.com

Here''s some pseudo-code that describes what I''m trying to do:

foreach(object in collection)
{
if (certain-condition)
collection.Remove(object);
}

The problem with this is that foreach gets messed up if anything is deleted
from the collection. What''s the best way to handle such a situation?

--
Robert W.
Vancouver, BC
www.mwtech.com

推荐答案

在VB 2005中,使用


for i作为integer in collection.count -1到0步-1

如果收集(i)=条件然后collection.removeat(i)

接下来我


迭代器不喜欢移除物体。 />

Mike Ober。


" Robert W." < Ro ***** @ discuss.microsoft.com写信息

新闻:96 *********************** *********** @ microsof t.com ...
In VB 2005, use

for i as Integer in collection.count -1 to 0 step -1
if collection(i) = condition then collection.removeat(i)
next i

Iterators don''t like removing objects.

Mike Ober.

"Robert W." <Ro*****@discussions.microsoft.comwrote in message
news:96**********************************@microsof t.com...

这里有一些描述我是什么的伪代码试图这样做:


foreach(收藏中的物件)

{

如果(特定条件)

collection.Remove(object);

}


这个问题是,如果有什么是
,foreach就会搞砸了
Here''s some pseudo-code that describes what I''m trying to do:

foreach(object in collection)
{
if (certain-condition)
collection.Remove(object);
}

The problem with this is that foreach gets messed up if anything is



从集合中删除

deleted


。处理这种情况的最佳方法是什么?


-

罗伯特W.

温哥华,不列颠哥伦比亚省
www.mwtech.com

from the collection. What''s the best way to handle such a situation?

--
Robert W.
Vancouver, BC
www.mwtech.com






在C#2003中?


-

Robert W. br />
温哥华,BC
www.mwtech.com


" Michael D. Ober"写道:
And in C# 2003 ?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Michael D. Ober" wrote:

在VB 2005中,使用


for i作为整数,在collection.count -1到0步-1

如果收集(i)=条件然后collection.removeat(i)

接下来我


迭代器不喜欢删除物品。


Mike Ober。


" Robert W." < Ro ***** @ discuss.microsoft.com写信息

新闻:96 *********************** *********** @ microsof t.com ...
In VB 2005, use

for i as Integer in collection.count -1 to 0 step -1
if collection(i) = condition then collection.removeat(i)
next i

Iterators don''t like removing objects.

Mike Ober.

"Robert W." <Ro*****@discussions.microsoft.comwrote in message
news:96**********************************@microsof t.com...

这里有一些描述我是什么的伪代码试图这样做:


foreach(收藏中的物件)

{

如果(特定条件)

collection.Remove(object);

}


这个问题是,如果有什么是
,foreach就会搞砸了
Here''s some pseudo-code that describes what I''m trying to do:

foreach(object in collection)
{
if (certain-condition)
collection.Remove(object);
}

The problem with this is that foreach gets messed up if anything is



从集合中删除

deleted


。处理这种情况的最佳方法是什么?


-

罗伯特W.

温哥华,不列颠哥伦比亚省
www.mwtech.com



" Robert W." < Ro ***** @ discussion.microsoft.com写信息

新闻:59 *********************** *********** @ microsof t.com ...
"Robert W." <Ro*****@discussions.microsoft.comwrote in message
news:59**********************************@microsof t.com...

> for i as Integer in collection.count -1到0步-1
如果collection(i)= condition然后collection.removeat(i)
next i
>for i as Integer in collection.count -1 to 0 step -1
if collection(i) = condition then collection.removeat(i)
next i


在C#2003中?
And in C# 2003 ?



哦,来吧。至少做一个 - 努力 - 。


///方舟

Oh, c''mon. At least make an -effort-.

///ark


这篇关于ForEach&amp;删除问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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