国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

輕松學習C#的ArrayList類

2020-01-24 01:22:06
字體:
來源:轉載
供稿:網友

動態數組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類的相關介紹,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 讷河市| 龙胜| 平陆县| 望谟县| 孝感市| 寿宁县| 育儿| 天峨县| 固原市| 黄骅市| 乐安县| 郸城县| 保康县| 邳州市| 沁源县| 澄江县| 临清市| 英德市| 什邡市| 桦南县| 东兰县| 龙里县| 齐河县| 晴隆县| 澄城县| 凤城市| 新泰市| 合山市| 宁远县| 南阳市| 望江县| 龙南县| 繁昌县| 阿尔山市| 苍南县| 洛阳市| 禹城市| 万载县| 寿光市| 德清县| 淳安县|