关于ArrayList()用法的实践理解

一、将数据Point[] arrPt 做为参数传给ArrayList.Add()时,只能使用arrlst[0],不能使用arrlst[1]、arrlst[2]等等,这样会出错;因为Point[] arrPt数组是做为一个引用传入给ArrayList的,所以显示出来的结果也是System.Drawing.Point[]类型。

实例如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 using System.Collections;
 8 
 9 namespace ArrayListDemo
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             
16             Point[] arrPt = { new Point(1,2),new Point(2,3),new Point(3,4),new Point(4,5),new Point(5,6)};
17             //Point[] arrpoint = {new Point(2,1),new Point(3,2),new Point(4,3) };
18 
19             ArrayList arrlst = new ArrayList();
20             arrlst.Add(arrPt);
21             //arrlst.Add(arrpoint);
22 
23             //Console.WriteLine(((Point[])arrlst[0])[1]);
24             //Console.WriteLine(arrPt[0]);
25             //Console.WriteLine(arrlst);
26             Console.WriteLine(arrlst[0]);
27             Console.WriteLine(arrlst[1]);
28             Console.WriteLine(arrlst[2]);
29         }
30     }
31 }

 

从上图结果可以看出,第27行,索引超出范围并报错。那如果在ArrayList()实例中正确显示出Point[] arrPt数组中的每个元素呢?应该使用如下方法:

     Console.WriteLine(((Point[])arrlst[0])[0]);
            Console.WriteLine(((Point[])arrlst[0])[1]);
            Console.WriteLine(((Point[])arrlst[0])[2]);
            Console.WriteLine(((Point[])arrlst[0])[3]);
            Console.WriteLine(((Point[])arrlst[0])[4]);

因为arrlst[0]是object类型,所以使用((Point[])arrlst[0])转换为Point[]数组类型,然后再使用(Point[])arrlst[0])[0]等数组索引就可以访问Point[] arrPt数组中的每个元素了;

具体实例如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 using System.Collections;
 8 
 9 namespace ArrayListDemo
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             
16             Point[] arrPt = { new Point(1,2),new Point(2,3),new Point(3,4),new Point(4,5),new Point(5,6)};
17             //Point[] arrpoint = {new Point(2,1),new Point(3,2),new Point(4,3) };
18 
19             ArrayList arrlst = new ArrayList();
20             arrlst.Add(arrPt);
21             //arrlst.Add(arrpoint);
22 
23             Console.WriteLine(((Point[])arrlst[0])[0]);
24             Console.WriteLine(((Point[])arrlst[0])[1]);
25             Console.WriteLine(((Point[])arrlst[0])[2]);
26             Console.WriteLine(((Point[])arrlst[0])[3]);
27             Console.WriteLine(((Point[])arrlst[0])[4]);
28             
29             //Console.WriteLine(arrlst);
30             Console.WriteLine(arrlst[0]);
31             //Console.WriteLine(arrlst[1]);
32             //Console.WriteLine(arrlst[2]);
33         }
34     }
35 }

 

 二、增加第29行代码,Console.WriteLine(arrlst);

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 using System.Collections;
 8 
 9 namespace ArrayListDemo
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             
16             Point[] arrPt = { new Point(1,2),new Point(2,3),new Point(3,4),new Point(4,5),new Point(5,6)};
17             //Point[] arrpoint = {new Point(2,1),new Point(3,2),new Point(4,3) };
18 
19             ArrayList arrlst = new ArrayList();
20             arrlst.Add(arrPt);
21             //arrlst.Add(arrpoint);
22 
23             Console.WriteLine(((Point[])arrlst[0])[0]);
24             Console.WriteLine(((Point[])arrlst[0])[1]);
25             Console.WriteLine(((Point[])arrlst[0])[2]);
26             Console.WriteLine(((Point[])arrlst[0])[3]);
27             Console.WriteLine(((Point[])arrlst[0])[4]);
28             
29             Console.WriteLine(arrlst);
30             Console.WriteLine(arrlst[0]);
31             //Console.WriteLine(arrlst[1]);
32             //Console.WriteLine(arrlst[2]);
33         }
34     }
35 }

 

对应的显示结果为:
System.Collections.ArrayList

第三,增加第17和第31行,Point[] arrpoint = {new Point(2,1),new Point(3,2),new Point(4,3) };   Console.WriteLine(arrlst[1]); arrlst.Add(arrpoint);

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Windows.Forms;
 7 using System.Collections;
 8 
 9 namespace ArrayListDemo
10 {
11     class Program
12     {
13         static void Main(string[] args)
14         {
15             
16             Point[] arrPt = { new Point(1,2),new Point(2,3),new Point(3,4),new Point(4,5),new Point(5,6)};
17             Point[] arrpoint = {new Point(2,1),new Point(3,2),new Point(4,3) };
18 
19             ArrayList arrlst = new ArrayList();
20             arrlst.Add(arrPt);
21             arrlst.Add(arrpoint);
22 
23             Console.WriteLine(((Point[])arrlst[0])[0]);
24             Console.WriteLine(((Point[])arrlst[0])[1]);
25             Console.WriteLine(((Point[])arrlst[0])[2]);
26             Console.WriteLine(((Point[])arrlst[0])[3]);
27             Console.WriteLine(((Point[])arrlst[0])[4]);
28             
29             Console.WriteLine(arrlst);
30             Console.WriteLine(arrlst[0]);
31             Console.WriteLine(arrlst[1]);
32             //Console.WriteLine(arrlst[2]);
33         }
34     }
35 }

 

posted @ 2022-06-03 22:32  chenlight  阅读(61)  评论(0编辑  收藏  举报