![]() |
#2
wangnannan2014-06-16 09:21
![]() for (int i = 0; i < mdownPoint.Count; i++)//此for语句是放在button控件button1_Click事件中的 你删除一次 mdownPoint.Count的count就不一样了 所以会出现这个问题 { if (mdownPoint[i].C== 1) { mdownPoint.RemoveAt(i); } } 可以考虑用递归方法 ![]() 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; } } } } } |

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 编辑 ]