【C#】List集合如何刪除對象
當前位置:點晴教程→知識管理交流
→『 技術文檔交流 』
//若是在循環的過程當中調用集合的remove()方法,就會致使循環出錯,例如:
for(int i=0;i<list.size();i++){
list.remove(...);
} 循環過程當中list.size()的大小變化了,就致使了錯誤。 正確的刪除作法是:svg Iterator it = list.iterator();
int index = 0;
while (it.hasNext())
{
Object obj = it.next();
if (needDelete(obj)) //needDelete返回boolean,決定是否要刪除
{
it.remove(); //todo delete
}
index ++;
}
該文章在 2021/3/10 9:07:28 編輯過 |
關鍵字查詢
相關文章
正在查詢... |