首页 > 编程 > C# > 正文

轻松学习C#的ArrayList类

2019-10-29 21:35:20
字体:
来源:转载
供稿:网友

轻松学习C#的ArrayList类,对C#的ArrayList类感兴趣的朋友可以参考本篇文章,帮助大家更灵活的运用C#的ArrayList类。

动态数组ArrayList类在System.Collecions的命名空间下,所以使用时要加入System.Collecions命名空间,而且ArrayList提供添加,插入或移除某一范围元素的方法。在ArrayList中,用户只能一次获取或设置一个元素的值。

一、ArrayList元素的添加

ArrayList提供了两种方法用于向ArrayList添加元素,即Add和AddRange。

(1),Add方法将单个元素添加到列表的尾部,其格式为:ArrayList 对象.Add(要添加的值)

(2),AddRange方法获取一个实现ICollection接口的集合实例,并将这个集合实例按顺序添加到列表的尾部,其格式为:ArrayList 对象.AddRange(要添加的数组)

例一、通过上述的方法对数组进行元素的添加和数组的添加

 

 
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections;//需要添加的命名空间  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.  
  8. namespace 动态数组的使用  
  9. {  
  10. class Program  
  11. {  
  12. static void Main(string[] args)  
  13. {  
  14. ArrayList al = new ArrayList(3);//定义的一个动态数组且初始数组元素个数为3个  
  15. Console.WriteLine("未添加前al的元素个数为:"+al.Count);  
  16. al.Add("abc");  
  17. al.Add("xyz");  
  18. al.Add("opq");  
  19. Console.WriteLine("调用Add方法后al的元素个数为:"+al.Count);  
  20. string[] last = { "def""ghj" };  
  21. al.AddRange(last);  
  22. Console.WriteLine("调用AddRange方法后al的元素个数为:"+al.Count);  
  23. foreach (string item in al)  
  24. {  
  25. Console.WriteLine(item);  
  26. }  
  27. Console.ReadLine();  
  28. }  
  29. }  
  30. }</span> 

输出的结果为:未添加前al的元素个数为:0

调用Add方法后al的元素个数为:3

调用AddRange方法后al的元素个数为:5

abc xyz opq def ghj(每一行输出一个)

二、ArrayList元素的删除

ArrayList提供了四种方法用于从ArrayList中删除元素。这四种方法是Remove,RemoveAt,RemoveRange方法和Clear方法。

Remove方法接受一个object类型值的参数,用于移除指定元素值的第一个匹配集合元素。其格式为:ArrayList 对象.Remove(值)

RemoveAt方法接受一个int类型的参数,用于删除指定索引的集合元素。其格式为:ArrayList 对象.RemoveAt(索引)

RemoveRange方法从集合中移除一定范围的元素。其格式为: ArrayList 对象.RemoveRange(开始索引,要删除的个数)

Clear方法清除所有的元素。

例二、用上述的方法实现对元素的删除

 

 
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections;//需要添加的命名空间  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.  
  8. namespace 动态数组的使用  
  9. {  
  10. class Program  
  11. {  
  12. static void Main(string[] args)  
  13. {  
  14. ArrayList al = new ArrayList(3);//定义的一个动态数组且初始数组元素个数为3个  
  15. al.Add("abc");  
  16. al.Add(50);  
  17. al.Add(10);  
  18. string[] last = { "def""ghj" };  
  19. al.AddRange(last);  
  20. Console.WriteLine("未删除前al的元素个数为:" + al.Count);  
  21. al.RemoveAt(2);//删除索引为2后的元素  
  22. Console.WriteLine("删除索引为2后的元素个数为:"+al.Count);  
  23. al.Remove("abc");//删除第一个值为abc的项  
  24. Console.WriteLine("删除值为abc后的元素个数为:"+al.Count);  
  25. al.RemoveRange(1,2);//删除自索引为1的两个元素  
  26. Console.WriteLine("删除自索引为1的两个元素后的元素个数:"+al.Count);  
  27. foreach (string item in al)//因为此对象中的元素类型不一致所以为object类型  
  28. {  
  29. Console.WriteLine(item);  
  30. }  
  31. Console.ReadLine();  
  32. }  
  33. }  
  34. }</span>  

输出的结果为:未删除前al的元素个数为:5

删除索引为2后的元素个数为:4

删除值为abc后的元素个数为:3

删除自索引为1的两个元素后的元素个数:1

xyz

三、ArrayList元素的查找

ArrayList元素的查找提供了三个方法查找ArrayList中的元素,分别是IndexOf方法,LastindexOf方法和BinarySearch方法。

(1)、IndexOf方法从前后搜素指定的字符串,如果找到,返回匹配的第一项的自0开始的索引,否则返回-1。其格式为:ArrayList 对象.IndexOf(要索引的字符串)

(2)、LastIndexOf方法从后向前搜素指定的字符串,如果找到,返回匹配的最后一项自0开始的索引,否则返回-1.其格式为:ArrayList 对象.LastIndexOf(要索引的字符串)

以上两个方法都有三个重载版本,表示从指定的索引处开始搜索或者是从指定索引处搜素指定长度的字符串。

(3)、BinarySearch方法使用二分算法从集合中指定的值,并返回找到的从0开始的索引,否则返回-1,其格式为:ArrayList 对象.BinarySearch(要索引的字符串)

例三、使用上述的方法查找指定的元素

 

 
  1. <span style="font-size:18px;">using System;  
  2. using System.Collections;//需要添加的命名空间  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7.  
  8. namespace 动态数组的使用  
  9. {  
  10. class Program  
  11. {  
  12. static void Main(string[] args)  
  13. {  
  14. string[] str = { "a""b""c""d""d""e""f" };  
  15. ArrayList al = new ArrayList(str);  
  16. int i = al.IndexOf("c");//查找第一个字符c在数组中的位置  
  17. Console.WriteLine("元素c在集合中的位置是:"+i);  
  18. i = al.LastIndexOf("d");//查找最后一个字符d在数组中的位置  
  19. Console.WriteLine("元素d在集合中的位置是:" + i);  
  20. int j = al.BinarySearch("f");//查找元素f在数组中的位置  
  21. if (j>0)  
  22. {  
  23. Console.WriteLine("元素f在数组中的位置是:"+j);  
  24. }  
  25. else 
  26. {  
  27. Console.WriteLine("没有找到a");  
  28. }  
  29. Console.ReadLine();  
  30. }  
  31. }  
  32. }</span>  

输出的结果为:元素c在集合中的位置是:2

元素d在集合中的位置是:3

元素f在数组中的位置是:5

四、ArrayList元素的遍历

在执行上述程序的过程中已经使用foreach语句进行ArrayList元素的遍历,在这里就不再举例进行说明。

以上就是关于C#的ArrayList类的相关介绍,希望对大家的学习有所帮助。


注:相关教程知识阅读请移步到c#教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表