在考试系统的调试过程中,需要对添加的试题进行移除。而由于试题是放置在arraylist的动态数组中的。一下就是这个用来循环移除的代码。这个代码看起来没有任何问题,但是由于arraylist在遍历过程中是无法对其进行修改的。所以总会出现无法进行下一次枚举的错误。
foreach (DataRow row in ltAllQuestion)
{
if (row["QuestionID"].ToString () == id)
{
ltAllQuestion.Remove(row);
}
}
//定义用来停止计数的标识
bool flag=false;
//定义用来计量循环次数的计数器
int countforremove=0;
foreach (DataRow row in ltAllQuestion)
{
if (row["QuestionID"].ToString () == id)
{
flag=true;
}
If(flag==false){
countforremove=countforremove+1;
}
}
针对:Arraylist集合无法修改,下一次枚举无法操作的解决方案
原文:http://blog.csdn.net/cfl20121314/article/details/18816079