C#集合的问题
我想问一下,我 往一个集合里边存东西,然后遍历它,如果条件满足我把它移除
但是,移除的时候,怎么才能不报错并且把集合中的每个元素都成功遍历! --------------------编程问答-------------------- C#提供的集合类已经提供好了这些功能。你用就是了。 --------------------编程问答-------------------- 不要用 foreach
用 for(int i = 最后一个, 反着干,
--------------------编程问答-------------------- 没有看明白你的意思。。。。 --------------------编程问答--------------------
--------------------编程问答-------------------- List<string> aa = new List<string>();
for (int i = dataGridView1.Rows.Count-1; i >=0 ; i--)
{
dataGridView1.Rows.RemoveAt(i); //删除行
}
aa.Add("abc");
aa.Add("123");
foreach (string x in aa)
{
if (x == "123")
{
aa.Remove(x);
}
} --------------------编程问答-------------------- 上边那个有点问题,你可以用这个试下,没问题的:
List<string> aa = new List<string>();
aa.Add("abc");
aa.Add("123");
for (int i=0;i<aa .Count ;i++)
{
if (aa[i] == "123")
{
aa.Remove(aa[i]);
}
}
补充:.NET技术 , C#