轻松学习C#的ArrayList类

动态数组ArrayList类在System.Collecions的命名空间下,所以使用时要加入System.Collecions命名空间,而且ArrayList提供添加,插入或移除某一范围元素的方法。在ArrayList中,用户只能一次获取或设置一个元素的值。
一、ArrayList元素的添加
         ArrayList提供了两种方法用于向ArrayList添加元素,即Add和AddRange。
         (1),Add方法将单个元素添加到列表的尾部,其格式为:ArrayList 对象.Add(要添加的值)
         (2),AddRange方法获取一个实现ICollection接口的集合实例,并将这个集合实例按顺序添加到列表的尾部,其格式为:ArrayList 对象.AddRange(要添加的数组)
例一、通过上述的方法对数组进行元素的添加和数组的添加

<span style="font-size:18px;">using System; 
using System.Collections;//需要添加的命名空间 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 动态数组的使用 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  ArrayList al = new ArrayList(3);//定义的一个动态数组且初始数组元素个数为3个 
  Console.WriteLine("未添加前al的元素个数为:"+al.Count); 
  al.Add("abc"); 
  al.Add("xyz"); 
  al.Add("opq"); 
  Console.WriteLine("调用Add方法后al的元素个数为:"+al.Count); 
  string[] last = { "def", "ghj" }; 
  al.AddRange(last); 
  Console.WriteLine("调用AddRange方法后al的元素个数为:"+al.Count); 
  foreach (string item in al) 
  { 
  Console.WriteLine(item); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</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方法清除所有的元素。
例二、用上述的方法实现对元素的删除

<span style="font-size:18px;">using System; 
using System.Collections;//需要添加的命名空间 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 动态数组的使用 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  ArrayList al = new ArrayList(3);//定义的一个动态数组且初始数组元素个数为3个 
  al.Add("abc"); 
  al.Add(50); 
  al.Add(10); 
  string[] last = { "def", "ghj" }; 
  al.AddRange(last); 
  Console.WriteLine("未删除前al的元素个数为:" + al.Count); 
  al.RemoveAt(2);//删除索引为2后的元素 
  Console.WriteLine("删除索引为2后的元素个数为:"+al.Count); 
  al.Remove("abc");//删除第一个值为abc的项 
  Console.WriteLine("删除值为abc后的元素个数为:"+al.Count); 
  al.RemoveRange(1,2);//删除自索引为1的两个元素 
  Console.WriteLine("删除自索引为1的两个元素后的元素个数:"+al.Count); 
  foreach (string item in al)//因为此对象中的元素类型不一致所以为object类型 
  { 
  Console.WriteLine(item); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</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(要索引的字符串)
 例三、使用上述的方法查找指定的元素

<span style="font-size:18px;">using System; 
using System.Collections;//需要添加的命名空间 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace 动态数组的使用 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  string[] str = { "a", "b", "c", "d", "d", "e", "f" }; 
  ArrayList al = new ArrayList(str); 
  int i = al.IndexOf("c");//查找第一个字符c在数组中的位置 
  Console.WriteLine("元素c在集合中的位置是:"+i); 
  i = al.LastIndexOf("d");//查找最后一个字符d在数组中的位置 
  Console.WriteLine("元素d在集合中的位置是:" + i); 
  int j = al.BinarySearch("f");//查找元素f在数组中的位置 
  if (j>0) 
  { 
  Console.WriteLine("元素f在数组中的位置是:"+j); 
  } 
  else 
  { 
  Console.WriteLine("没有找到a"); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</span> 

输出的结果为:元素c在集合中的位置是:2
                       元素d在集合中的位置是:3
                       元素f在数组中的位置是:5
四、ArrayList元素的遍历
在执行上述程序的过程中已经使用foreach语句进行ArrayList元素的遍历,在这里就不再举例进行说明。

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

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。