比如 
class lei
{
  int a1;
  string a2;
}
list<lei> leiList=new list<lei>()
        private void button1_Click(object sender, EventArgs e)
        {
            for(int i=0;i<10;i++) 
              {
                  lei tmp=new lei();
                  tmp.a1=i;
                   leiList.Add(tmp)
               }
         }        private void button3_Click(object sender, EventArgs e)
        {
              for(int i=0;i<leiList.Count;i++)
             {
               // if(leiList[i].a1>3) leiList.RemoveAt(i);
              //或 tmp=leiList[i];if(tmp.a1>3) leiList.RemoveAt(tmp);
                }
         }
   这样貌似都不对。

解决方案 »

  1.   

    for的时候 i从大到小的循环
      

  2.   

      for(int i=leiList.Count-1;i >-1;i--)
      {
           if(leiList[i].a1>3)
          { 
             leiList.RemoveAt(i);
                  
           } 
      

  3.   

    从小到大的移除会有个问题就是,比如list[4]>3,list[4]被移除后,list.count()就减少一位,这样最后的几个就不会被移除了
      

  4.   


    foreach(YourClass cl in YourClassListObj)
    {
        if("条件")
        {
            YourClassListObj.Remove(cl);
        }
    }
      

  5.   

     List<int> li = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
                List<int> sub = li.FindAll(DualNumber);
                foreach (var di in sub)
                {
                    Console.WriteLine(di);
                }            li.RemoveAll(DualNumber);
                foreach (var i in li)
                {
                    Console.WriteLine(i);
                }static bool DualNumber(int v)
            {
                if (v % 2 == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
      

  6.   

    或者
    li.RemoveAll(i => (i % 2) == 0);
      

  7.   

    采用笨方法吧,设一个局部的List变量
     private void button3_Click(object sender, EventArgs e) 

          List<Lei> TempList = new List<lei>();
          TempList.AddRange(leiList);   // 复制到局部变量
                  
          for(int i=0;i <TempList.Count;i++) 
          { 
                 tmp=TempList[i];
                 if(tmp.a1>3) leiList.RemoveAt(tmp); 
          } 

      

  8.   

    leiList.RemoveAll(le => le.a1 >= 3);
      

  9.   

    for(int i=leiList.Count-1;i <0;i--) 有删除操作的,应该是这样循环滴
      

  10.   


                leiList.RemoveAll(
                    delegate(Lei l)
                    {
                        return l.a1 > 3;
                    }
                );