日期:2014-05-18  浏览次数:20801 次

关于Arrayline的元素
public class Line
  {
  public Point StartPoint = new Point();
  public Point EndPoint = new Point();  
  }
ArrayList lines = new ArrayList();
Line line =new Line();
  line.StartPoint.X = 0;
  line.StartPoint.Y = 0;
  line.EndPoint.X = 100;
  line.EndPoint.Y = 100;
  lines.Add(line);
  line.StartPoint.X = 1;
  line.StartPoint.Y = 1;
  line.EndPoint.X = 101;
  line.EndPoint.Y = 101;
  lines.Add(line);
为什么我这样写
用foreach (Line line in lines)遍历lines或者用for(int i=0;i<lines.Count;i++)遍历lines
取得的line或者lines[i]的都是一样的?


------解决方案--------------------
探讨
引用:
ArrayList lines = new ArrayList(); 
Line line =new Line(); 
line.StartPoint.X = 0; 
line.StartPoint.Y = 0; 
line.EndPoint.X = 100; 
line.EndPoint.Y = 100; 
lines.Add(line); 
Line line =new Line(); 《----这里,每次你都需要再建一个Line对象
line = new Line();//这么简单的错误你还看不出来么?
line.StartPoint.X = 1; 
line.StartPoint.Y = 1; 
line.EndPoint.X = 101; 
line.EndPoint.Y = 101; 
lines.Add(line); 

 
这…

------解决方案--------------------
Line line =new Line();
==>
line =new Line();