当前位置: 移动技术网 > IT编程>开发语言>c# > 轻松学习C#的ArrayList类

轻松学习C#的ArrayList类

2019年07月18日  | 移动技术网IT编程  | 我要评论
动态数组arraylist类在system.collecions的命名空间下,所以使用时要加入system.collecions命名空间,而且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类的相关介绍,希望对大家的学习有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网