复制列表时列出明确的问题 [英] List clear problem when copy a list

查看:44
本文介绍了复制列表时列出明确的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个绘图项目。在下面的代码中,我处理临时列表中的新点,当mouseup事件引发时,这些点复制了另一个处理所有点列表的列表。但是当临时列表清除时,它会同时从点列表中清除。所以积分列表总是空的。如何解决?



列表< List< Point>> points = new List< List< Point>>(); 
列表<点> tempPoints = new List< Point>();
bool startDrawing;

public Form1()
{
InitializeComponent();
this.Paint + = Form1_Paint;
this.MouseMove + = Form1_MouseMove;
this.MouseDown + = Form1_MouseDown;
this.MouseUp + = Form1_MouseUp;
}

void Form1_MouseUp(对象发送者,MouseEventArgs e)
{
startDrawing = false;
points.Add(tempPoints);
tempPoints.Clear();
}

void Form1_MouseDown(对象发送者,MouseEventArgs e)
{
startDrawing = true;
}

void Form1_MouseMove(对象发送者,MouseEventArgs e)
{
if(startDrawing)
tempPoints.Add(e.Location);
}

void Form1_Paint(对象发送者,PaintEventArgs e)
{
foreach(List< Point> list in points)
{
if(list.Count> = 2)
e.Graphics.DrawLines(Pens.Red,list.ToArray());
}
if(tempPoints.Count> = 2& startDrawing)
{
e.Graphics.DrawLines(Pens.Red,tempPoints.ToArray());
}
}

private void timer1_Tick(object sender,EventArgs e)
{
this.Invalidate();
}

解决方案

问题是你没有将积分本身添加到列表中,而是你是添加整个临时列表。之后清除的相同列表。不要清除临时列表,而是创建一个新列表:

  void  Form1_MouseUp( object  sender,MouseEventArgs e)
{
startDrawing = false ;
points.Add(tempPoints);
// tempPoints.Clear(); - 不,这会清除您刚刚添加到积分的列表
tempPoints = new 列表< Point>(); // 是的,创建一个新列表来存储新点
}


当你将tempPoints添加到另一个列表时,它只是添加引用,所以你需要先将它克隆,然后再添加到点。见下文



列表< Point> tempPoints1 = tempPoints.Select(point = > (Point)point.Clone())。ToList()

points.Add(tempPoints1 );


I am working on a drawing project. In the following code I handle new points in a temporary list and when mouseup event raised these points copied another list that handles all point lists.But when temporary list cleared, it is cleared from points list at the same time. So points list is always empty. How can it be fixed?

List<List<Point>> points = new List<List<Point>>();
List<Point> tempPoints = new List<Point>();
bool startDrawing;

public Form1()
{
    InitializeComponent();
    this.Paint += Form1_Paint;
    this.MouseMove += Form1_MouseMove;
    this.MouseDown += Form1_MouseDown;
    this.MouseUp += Form1_MouseUp;
}

void Form1_MouseUp(object sender, MouseEventArgs e)
{
    startDrawing = false;
    points.Add(tempPoints);
    tempPoints.Clear();
}

void Form1_MouseDown(object sender, MouseEventArgs e)
{
    startDrawing = true;
}

void Form1_MouseMove(object sender, MouseEventArgs e)
{
    if (startDrawing)
    tempPoints.Add(e.Location);
}

void Form1_Paint(object sender, PaintEventArgs e)
{
    foreach (List<Point> list in points)
    {
        if (list.Count >= 2)
            e.Graphics.DrawLines(Pens.Red, list.ToArray());
    }
    if (tempPoints.Count >= 2 & startDrawing)
    {
        e.Graphics.DrawLines(Pens.Red, tempPoints.ToArray());
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.Invalidate();
}

解决方案

The problem is you are not adding the points themselves to the list but instead you are adding the whole temp list. The same list of points that you clear afterwards. Do not clear the temp list, create a new list instead:

void Form1_MouseUp(object sender, MouseEventArgs e)
{
    startDrawing = false;
    points.Add(tempPoints);
    // tempPoints.Clear(); - no, this clears the list you just added to points
    tempPoints = new List<Point>(); // yes, create a new list to store the new points
}


When you add tempPoints to another list it just add the reference so you need to first clone it before adding it to points. See below

List<Point> tempPoints1=tempPoints.Select(point=> (Point)point.Clone()).ToList()

points.Add(tempPoints1);


这篇关于复制列表时列出明确的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆