在foreach()循环中删除ArrayList [n]? [英] remove ArrayList[n] inside foreach() loop?

查看:128
本文介绍了在foreach()循环中删除ArrayList [n]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想浏览ArrayList的每个条目(?)并删除一些特定的

条目。所以我尝试了以下但它在运行时抛出异常:


foreach(myArrayList中的myEntry条目)

{

// do某事......


if(entry.fieldA == 0)

{//删除条目

myArrayList.Remove(条目); //以下例外

} //删除条目

}


/ *例外:

System.InvalidOperationException:集合已被修改;枚举

操作可能无法执行。

在System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()

* /


所以我必须创建一个索引数组来删除和删除foreach()循环之外的索引条目

。这是一种自然的方式吗?还是有更好的方式?b / b $

问候


鲍勃

Hi all,

I wanted to go through each entry(?) of ArrayList and remove some particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a better
way?

Regards

Bob

推荐答案




" bbg" < bb*@discussions.microsoft.com写信息

新闻:0F *************************** ******* @ microsof t.com ...
Hi,

"bbg" <bb*@discussions.microsoft.comwrote in message
news:0F**********************************@microsof t.com...

大家好,


我想去通过ArrayList的每个条目(?)并删除一些

特定的

条目。所以我尝试了以下但它在运行时抛出异常:


foreach(myArrayList中的myEntry条目)

{

// do某事......


if(entry.fieldA == 0)

{//删除条目

myArrayList.Remove(条目); //以下例外

} //删除条目

}


/ *例外:

System.InvalidOperationException:集合已被修改;枚举

操作可能无法执行。

在System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()

* /


所以我必须创建一个索引数组来删除和删除foreach()循环之外的索引条目

。这是一种自然的方式吗?或者是否有更好的方式?b $ b方式?
Hi all,

I wanted to go through each entry(?) of ArrayList and remove some
particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a
better
way?



你不能修改你正在迭代的集合。你必须保持

跟踪你想要删除的那些元素并在之后删除它们

集合是迭代的。一种方法是

ArrayList toremove = new ...

foreach(myArrayList中的myEntry条目)

{

//做点什么......


if(entry.fieldA == 0)

toremove.Add(entry)

}

foreach(myEntry条目inremove)

myArrayList.Remove(条目);

You cannot modify the collection you are iterating in. You have to keep
track of those elements that you want removed and remove them after the
colection is iterate. A way of doing it is
ArrayList toremove = new ...
foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
toremove.Add( entry)
}
foreach(myEntry entry in toremove )
myArrayList.Remove( entry);


或者,如果你想要防止必须进行两次迭代,你可以只需要
向后遍历列表(这样你就可以一直遍历

项目列表,当你删除它们)并根据需要删除项目:


for(int index = myArrayList.Count - 1; index> = 0; index--)

{

//获取物品。

myEntry entry =(myEntry)myArrayList [index];


//检查删除。

if(entry.fieldA == 0)

{

//删除。

myArrayList.RemoveAt(index);

}

}

-

- Nicholas Paldi没有[.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


Ignacio Machin(.NET / C#MVP)" < machin TA laceupsolutions.com写在

消息新闻:%2 **************** @ TK2MSFTNGP06.phx.gbl ...
Or, if you want to prevent having to make two iterations, you could just
iterate backwards through the list (so you can consistently traverse the
items in the list as you remove them) and remove the items as needed:

for (int index = myArrayList.Count - 1; index >= 0; index--)
{
// Get the item.
myEntry entry = (myEntry) myArrayList[index];

// Check to remove.
if (entry.fieldA == 0)
{
// Remove.
myArrayList.RemoveAt(index);
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ignacio Machin ( .NET/ C# MVP )" <machin TA laceupsolutions.comwrote in
message news:%2****************@TK2MSFTNGP06.phx.gbl...




" bbg" < bb*@discussions.microsoft.com写信息

新闻:0F *************************** ******* @ microsof t.com ...
Hi,

"bbg" <bb*@discussions.microsoft.comwrote in message
news:0F**********************************@microsof t.com...

>大家好,

我想通过每个输入(?)ArrayList并删除一些特定的
条目。所以我尝试了以下但它在运行时抛出异常:

foreach(myArrayList中的myEntry条目)
{
//做点什么......

if(entry.fieldA == 0)
{//删除条目
myArrayList.Remove(entry); //例外下面
} //删除条目
}
/ *例外:
System.InvalidOperationException:集合被修改;枚举
操作可能无法执行。
在System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
* /

所以我不得不创建一个索引数组删除和删除索引条目
在foreach()循环之外。这是一种自然的方式吗?还是有更好的方式?
>Hi all,

I wanted to go through each entry(?) of ArrayList and remove some
particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a
better
way?



你不能修改你正在迭代的集合。你必须保持

跟踪你想要删除的那些元素并在之后删除它们

集合是迭代的。一种方法是

ArrayList toremove = new ...

foreach(myArrayList中的myEntry条目)

{

//做点什么......


if(entry.fieldA == 0)

toremove.Add(entry)

}

foreach(myEntry条目inremove)

myArrayList.Remove(条目);


You cannot modify the collection you are iterating in. You have to keep
track of those elements that you want removed and remove them after the
colection is iterate. A way of doing it is
ArrayList toremove = new ...
foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
toremove.Add( entry)
}
foreach(myEntry entry in toremove )
myArrayList.Remove( entry);



2007年8月27日星期一13:44:00 -0700,bbg

< bb*@discussions.microsoft.comwrote:



与Ignacio相同的主题,

但是我很想将想要的成员保存到新列表中

消除删除步骤。

取决于我想要制作一个''myEntry'以及

从好到坏的比例是多么昂贵。

Bob C.
On Mon, 27 Aug 2007 13:44:00 -0700, bbg
<bb*@discussions.microsoft.comwrote:
Hi,
Same theme as Ignacio,
But I would be tempted to save the wanted members to a new list
eliminating the remove step.
Depends I guess on how expensive it is to make a ''myEntry'' and the
ratio of good to bad.
Bob C.

>大家好,

我想浏览ArrayList的每个条目(?)和删除一些特定的条目。所以我尝试了以下但它在运行时抛出异常:


foreach(myArrayList中的myEntry条目)

{

// do某事......


if(entry.fieldA == 0)

{//删除条目

myArrayList.Remove(条目); //以下例外

} //删除条目

}

/ *例外:
System.InvalidOperationException:集合被修改;枚举
操作可能无法执行。

在System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
* /

所以我必须创建一个索引数组,用于删除和删除foreach()循环之外的索引条目。这是一种自然的方式还是有更好的方式?

鲍勃
>Hi all,

I wanted to go through each entry(?) of ArrayList and remove some particular
entry. So I tried following but it throws exception at runtime:

foreach (myEntry entry in myArrayList)
{
// do something...

if (entry.fieldA == 0)
{ // remove entry
myArrayList.Remove(entry); // exception below
} // remove entry
}

/* exception:
System.InvalidOperationException: Collection was modified; enumeration
operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSi mple.MoveNext()
*/

So I had to create an index array to remove and remove indexed entries
outside of foreach() loop. Is this a natural way to do or is there a better
way?

Regards

Bob


这篇关于在foreach()循环中删除ArrayList [n]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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