注册 登录
编程论坛 C# 论坛

怎么一次性删除结构体 List 中满足条件某值的所有元素?

gxlove 发布于 2014-06-14 21:46, 5471 次点击
程序代码:

public struct Path
        {
            public  int A;
            public int B;
            public int C;
        }

 List<Path> mdownPoint = new List<Path>();
....
例如:其中mdownPoint 中有四个元素分别为{1,0,1}{0,3,1}{2,0,1} {5,8,1} ,用以下循环查找 元素C的值都为1的全删除, 可是点一次button按钮只能删除第一和第三个元素,然后再按第二次button可删除第二个,接着再按button才可全部删除完,如果元素更多时要按更多次button方可全删除。不懂为什么?
  for (int i = 0; i < mdownPoint.Count; i++)//此for语句是放在button控件button1_Click事件中的
            {
                if (mdownPoint[i].C== 1)
                {
                    mdownPoint.RemoveAt(i);
                }
            }


[ 本帖最后由 gxlove 于 2014-6-15 12:36 编辑 ]
5 回复
#2
wangnannan2014-06-16 09:21
程序代码:
  for (int i = 0; i < mdownPoint.Count; i++)//此for语句是放在button控件button1_Click事件中的
            {
                if (mdownPoint[i].C== 1)
                {
                    mdownPoint.RemoveAt(i);
                }
            }
你删除一次  mdownPoint.Count的count就不一样了 所以会出现这个问题
可以考虑用递归方法
程序代码:

using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication7
{
    public struct Path
        {
            public  int A;
            public int B;
            public int C;
        }


    public partial class Form1 : Form
    {
        List<Path> mdownPoint = new List<Path>() {};
        public Form1()
        {
            InitializeComponent();
            this.Load += new EventHandler(Form1_Load);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            Path p = new Path();
            p.A = 1;
            p.B = 0;
            p.C = 1;
            mdownPoint.Add(p);

            Path p1 = new Path();
            p1.A = 0;
            p1.B = 3;
            p1.C = 1;
            mdownPoint.Add(p1);

            Path p2 = new Path();
            p2.A = 2;
            p2.B = 0;
            p2.C = 1;
            mdownPoint.Add(p2);

            Path p3 = new Path();
            p3.A = 5;
            p3.B = 8;
            p3.C = 1;
            mdownPoint.Add(p3);

        }

      

        private void button1_Click(object sender, EventArgs e)
        {
            op(mdownPoint.Count);
            int a = mdownPoint.Count;//此处可以下断点监视查看一下删除的结果是否正确
        }


        private void op(int ccount)//ccount 是mdownPoint.Count的数量
        {
            for (int i = 0; i < ccount; i++)//此for语句是放在button控件button1_Click事件中的
            {

                if (mdownPoint[i].C == 1)
                {
                    mdownPoint.RemoveAt(i);
                    op(mdownPoint.Count);
                    break;
                }
            }
        }

    }
}


#3
wangnannan2014-06-16 09:25
还有一种方法 就是用goto 这也是我喜欢用的 你也可以参考一下
程序代码:

        private void button1_Click(object sender, EventArgs e)
        {
            rstart:
            for (int i = 0; i < mdownPoint.Count; i++)//此for语句是放在button控件button1_Click事件中的
            {
      
                if (mdownPoint[i].C == 1)
                {
                    mdownPoint.RemoveAt(i);
                    goto rstart;
                }
            }
            int a = mdownPoint.Count;//此处可以用监视查看一下删除的结果是否正确
        }

#4
gxlove2014-06-16 15:12
回复 3 楼 wangnannan
谢谢wangnannan的回复,觉得最后一种简洁。以下方法类似于最后一种,不过更简洁,代码如下:            
       for (int i = 0; i < mdownPoint.Count; i++)
            {

                if (mdownPoint[i].C== 1)
                {
                    mdownPoint.RemoveAt(i);
                    --i;
                }
            }
#5
wangnannan2014-06-16 15:14
回复 4 楼 gxlove
挺好 挺好 楼主会举一反三了 学编程就该这样
#6
gxlove2014-06-16 17:00
回复 5 楼 wangnannan
还得谢谢wangnannan提供思路!学习了。
1