关于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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端