2016-09-15 58 views
0

如何創建點的多維列表?我GOOGLE了很多,但我沒有找到一個工作解決方案。多維點列表

例如:

List<Point> lines = new List<Point>(new Point[] {}); 

Point[] points = {new Point(0, 1), new Point(2, 3)}; 
lines.AddRange(points); 

MessageBox.Show(lines[0][1].X.ToString()); 

什麼是我的錯?

在此先感謝。

+4

你需要一個'清單',而不是'列表' – haim770

+0

@ haim770謝謝。 – Reese

+0

@Reese如果你找到了答案,那麼標記是綠色的或者你自己的答案並且標記爲綠色。 –

回答

2

這是一個字符串的多維表的例子:

 List<List<string>> matrix = new List<List<string>>(); 
     List<string> track = new List<string>(); 
     track.Add("2349"); 
     track.Add("Test 123"); 
     matrix.Add(track); 

     MessageBox.Show(matrix[0][1]); 

或者你的情況:

 Point p1 = new Point(); // create new Point 
     p1.x = 5; 
     p1.y = 10; 

     List<List<Point>> points = new List<List<Point>>(); // multidimensional list of poits 

     List<Point> point = new List<Point>(); 
     point.Add(p1); // add a point to a list of point 

     points.Add(point); // add that list point to multidimensional list of points 

     MessageBox.Show(points[0][0].x); // read index 0 "(list<point>)" take index 0 of that list "(Point object)" take the value x. "(p1.x)" 
3

看看這有助於:

List<List<Point>> lines = new List<List<Point>>(); 

List<Point> points1 = new List<Point> { new Point(0, 1), new Point(2, 3) }; 
List<Point> points2 = new List<Point> { new Point(0, 1), new Point(2, 3) }; 
lines.Add(points1); 
lines.Add(points2); 

MessageBox.Show(lines[0][1].X.ToString()); 
MessageBox.Show(lines[1][1].X.ToString());