arraylist已经过时,你可以使用List<Point>或者List<PointF> 代码如下: List<Point> list = new List<Point>() { new Point() { X = 1, Y = 1 }, new Point() { X = 2, Y = 2 }, ... }; list.Add(new Point(100, 100)); list.Add(new Point(101, 101)); ... foreach (Point p in list) { Console.WriteLine("x = {0}, y = {1}.", p.X, p.Y); }
ArrayList不要用了,装箱拆箱的,用List吧 Point p = new Point(); p.X = 1; p.Y = 2;List<Point> list = new List<Point>(); list.Add(p); foreach (Point pp in list) { Console.WriteLine(pp.X.ToString()); }
不用Consol.WriteLine,怎么把List中数据取出来?
ArrayList a = new ArrayList(); string[,] arr = new string[10, 10]; for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { arr[i, j] = Convert.ToString(i + j); } } a.Add(arr);//存 string[,] b = a[0] as string[,];//取
foreach (Point p in list) { //p已经取出来了。 int x = P.X; int y = p.Y; }
麻烦你一下,如果我 private int[,] VirtualMap= new int[9, 9];把VirtualMap的坐标存入list,再取出来 那么代码怎么写呢?还有为什么不用ArrayList呢?
不用arraylist是因为arraylist元素存取涉及到装箱拆箱有一定的性能损耗 而不用数组是因为List用起来更方便,容量不用操心,访问方便 坐标用Point结构再合适不过了,楼上不都有存取的吗? Point p = new Point(); p.X = 10; p.Y = 20;List<Point> list = new List<Point>();list.Add(p);//存 int x = list[0].X;//取第一个坐标点的X坐标 int y = list[0].Y;//取第一个坐标点的Y坐标
代码如下:
List<Point> list = new List<Point>()
{
new Point() { X = 1, Y = 1 },
new Point() { X = 2, Y = 2 },
...
};
list.Add(new Point(100, 100));
list.Add(new Point(101, 101));
...
foreach (Point p in list)
{
Console.WriteLine("x = {0}, y = {1}.", p.X, p.Y);
}
Point p = new Point();
p.X = 1;
p.Y = 2;List<Point> list = new List<Point>();
list.Add(p);
foreach (Point pp in list)
{
Console.WriteLine(pp.X.ToString());
}
string[,] arr = new string[10, 10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
arr[i, j] = Convert.ToString(i + j);
}
}
a.Add(arr);//存
string[,] b = a[0] as string[,];//取
{
//p已经取出来了。
int x = P.X;
int y = p.Y;
}
那么代码怎么写呢?还有为什么不用ArrayList呢?
不用arraylist是因为arraylist元素存取涉及到装箱拆箱有一定的性能损耗
而不用数组是因为List用起来更方便,容量不用操心,访问方便
坐标用Point结构再合适不过了,楼上不都有存取的吗?
Point p = new Point();
p.X = 10;
p.Y = 20;List<Point> list = new List<Point>();list.Add(p);//存
int x = list[0].X;//取第一个坐标点的X坐标
int y = list[0].Y;//取第一个坐标点的Y坐标