【发布时间】:2016-02-01 12:17:11
【问题描述】:
我正在尝试修改 for 值中的列表
for (int i = 0; i < theList.Count; i++) {
if(someCircunstances)
theList.remove(component);
else
theList.add(component);
}
我用这种方法得到一个ArgumentOutOfRangeException。
有什么方法可以实现吗?
【问题讨论】:
-
您可能没有对
component对象的正确引用。你是如何看待这个参考的? -
我做的,当第五次迭代进来时它崩溃了,我调试了看到崩溃与删除和添加函数无关
-
需要反转for循环从Count -1到0开始
-
你的意思是
for(int i = theList.Count - 1; i < 0; i++) -
你在迭代过程中添加的项目呢?你也想遍历它们吗?