枚举List<>的最佳方法&安培;删除不需要的元素 [英] best way to enumerate List<> & remove unwanted elements?

查看:79
本文介绍了枚举List<>的最佳方法&安培;删除不需要的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然你不能只使用一个简单的for循环,因为你可以跳过

元素。


你可以修改每个循环计数器时间元素被删除。

但是,必须在每次迭代时检查循环结束条件,因为当删除元素时,计数会发生变化,因此需要检查
。我认为这是每次保证计算的
,而不是缓存。


那么,这是最好的方法吗?


List< intmylist = ....... something ......;

for(int i = 0; i< mylist.Count; i ++){

if(want_to_remove){

mylist.RemoveAt(i);

i - ;

}

}


Zytan

Obviously you can''t just use a simple for loop, since you may skip
over elements.

You could modify the loop counter each time an element is deleted.
But, the loop ending condition must be checked on each iteration,
since the Count changes as you delete elements. I would think it is
guaranteed to be computed each time, and not cached.

So, is this the best way?

List<intmylist = .......something......;
for (int i=0; i<mylist.Count; i++) {
if (want_to_remove) {
mylist.RemoveAt(i);
i--;
}
}

Zytan

推荐答案

不,它不是' T。为了使它更容易,你真的应该从列表的

结尾枚举。这样,你就不必玩指数

变量(在这种情况下,我)。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Zytan" < zy ********** @ gmail.comwrote in message

news:11 ******************** **@u30g2000hsc.googlegr oups.com ...
No, it isn''t. To make it easier, you really should enumerate from the
end of the list. This way, you don''t have to play around with the index
variable (in this case, i).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Zytan" <zy**********@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...

显然你不能只使用简单的for循环,因为你可以跳过

over elements。


每次删除元素时都可以修改循环计数器。

但是,必须在每个元素上检查循环结束条件迭代,

,因为当你删除元素时,Count会改变。我认为这是每次保证计算的
,而不是缓存。


那么,这是最好的方法吗?


List< intmylist = ....... something ......;

for(int i = 0; i< mylist.Count; i ++){

if(want_to_remove){

mylist.RemoveAt(i);

i - ;

}

}


Zytan
Obviously you can''t just use a simple for loop, since you may skip
over elements.

You could modify the loop counter each time an element is deleted.
But, the loop ending condition must be checked on each iteration,
since the Count changes as you delete elements. I would think it is
guaranteed to be computed each time, and not cached.

So, is this the best way?

List<intmylist = .......something......;
for (int i=0; i<mylist.Count; i++) {
if (want_to_remove) {
mylist.RemoveAt(i);
i--;
}
}

Zytan



不,它不是。为了方便起见,你真的应该从列表的
No, it isn''t. To make it easier, you really should enumerate from the

结尾进行枚举。这样,您就不必使用索引

变量(在本例中为i)。
end of the list. This way, you don''t have to play around with the index
variable (in this case, i).



好​​的。用for循环枚举?我知道,自动i--每次都是足够的,不会跳过元素。谢谢。


(我相信我的解决方案仍然可以,但是,对吗?这不是

最佳方式。)


Zytan

Ok. Enumerate with a for loop? And the automatic i-- each time is
sufficient for not skipping elements, I see. Thanks.

(I believe my solution still works, though, right? It''s just not the
best way.)

Zytan




" Zytan" < zy ********** @ gmail.comwrote in message

news:11 ******************** **@u30g2000hsc.googlegr oups.com ...

"Zytan" <zy**********@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...

显然你不能只使用简单的for循环,因为你可以跳过

over elements。


每次删除元素时都可以修改循环计数器。

但是,必须在每个元素上检查循环结束条件迭代,

,因为当你删除元素时,Count会改变。我认为这是每次保证计算的
,而不是缓存。


那么,这是最好的方法吗?


List< intmylist = ....... something ......;

for(int i = 0; i< mylist.Count; i ++){

if(want_to_remove){

mylist.RemoveAt(i);

i - ;

}

}
Obviously you can''t just use a simple for loop, since you may skip
over elements.

You could modify the loop counter each time an element is deleted.
But, the loop ending condition must be checked on each iteration,
since the Count changes as you delete elements. I would think it is
guaranteed to be computed each time, and not cached.

So, is this the best way?

List<intmylist = .......something......;
for (int i=0; i<mylist.Count; i++) {
if (want_to_remove) {
mylist.RemoveAt(i);
i--;
}
}



不,你不会减少(i)那样,你最终会在

即RemoveAt(i)。


你只想做mylist.Clear如果你只想*清除*所有元素

out如果这就是你要做的事情。

No, you don''t decrement (i) like that, you''re going to eventually blow up at
that RemoveAt(i).

You just want to do mylist.Clear if you just want to *clear* all elements
out of the list, if that''s what you''re trying to do.


这篇关于枚举List&lt;&gt;的最佳方法&安培;删除不需要的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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