雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C#教程

當前位置:主頁 > 軟件編程 > C#教程 >

輕松學習C#的ArrayList類

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C#教程|點擊:

動態(tài)數(shù)組ArrayList類在System.Collecions的命名空間下,所以使用時要加入System.Collecions命名空間,而且ArrayList提供添加,插入或移除某一范圍元素的方法。在ArrayList中,用戶只能一次獲取或設置一個元素的值。
一、ArrayList元素的添加
         ArrayList提供了兩種方法用于向ArrayList添加元素,即Add和AddRange。
         (1),Add方法將單個元素添加到列表的尾部,其格式為:ArrayList 對象.Add(要添加的值)
         (2),AddRange方法獲取一個實現(xiàn)ICollection接口的集合實例,并將這個集合實例按順序添加到列表的尾部,其格式為:ArrayList 對象.AddRange(要添加的數(shù)組)
例一、通過上述的方法對數(shù)組進行元素的添加和數(shù)組的添加

<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 動態(tài)數(shù)組的使用 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  ArrayList al = new ArrayList(3);//定義的一個動態(tài)數(shù)組且初始數(shù)組元素個數(shù)為3個 
  Console.WriteLine("未添加前al的元素個數(shù)為:"+al.Count); 
  al.Add("abc"); 
  al.Add("xyz"); 
  al.Add("opq"); 
  Console.WriteLine("調用Add方法后al的元素個數(shù)為:"+al.Count); 
  string[] last = { "def", "ghj" }; 
  al.AddRange(last); 
  Console.WriteLine("調用AddRange方法后al的元素個數(shù)為:"+al.Count); 
  foreach (string item in al) 
  { 
  Console.WriteLine(item); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</span> 

輸出的結果為:未添加前al的元素個數(shù)為:0
                       調用Add方法后al的元素個數(shù)為:3 
                       調用AddRange方法后al的元素個數(shù)為:5
                        abc  xyz  opq  def  ghj(每一行輸出一個)
二、ArrayList元素的刪除
        ArrayList提供了四種方法用于從ArrayList中刪除元素。這四種方法是Remove,RemoveAt,RemoveRange方法和Clear方法。
        Remove方法接受一個object類型值的參數(shù),用于移除指定元素值的第一個匹配集合元素。其格式為:ArrayList 對象.Remove(值)
        RemoveAt方法接受一個int類型的參數(shù),用于刪除指定索引的集合元素。其格式為:ArrayList 對象.RemoveAt(索引)
        RemoveRange方法從集合中移除一定范圍的元素。其格式為: ArrayList 對象.RemoveRange(開始索引,要刪除的個數(shù))
        Clear方法清除所有的元素。
例二、用上述的方法實現(xiàn)對元素的刪除

<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 動態(tài)數(shù)組的使用 
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  ArrayList al = new ArrayList(3);//定義的一個動態(tài)數(shù)組且初始數(shù)組元素個數(shù)為3個 
  al.Add("abc"); 
  al.Add(50); 
  al.Add(10); 
  string[] last = { "def", "ghj" }; 
  al.AddRange(last); 
  Console.WriteLine("未刪除前al的元素個數(shù)為:" + al.Count); 
  al.RemoveAt(2);//刪除索引為2后的元素 
  Console.WriteLine("刪除索引為2后的元素個數(shù)為:"+al.Count); 
  al.Remove("abc");//刪除第一個值為abc的項 
  Console.WriteLine("刪除值為abc后的元素個數(shù)為:"+al.Count); 
  al.RemoveRange(1,2);//刪除自索引為1的兩個元素 
  Console.WriteLine("刪除自索引為1的兩個元素后的元素個數(shù):"+al.Count); 
  foreach (string item in al)//因為此對象中的元素類型不一致所以為object類型 
  { 
  Console.WriteLine(item); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</span> 

輸出的結果為:未刪除前al的元素個數(shù)為:5
                       刪除索引為2后的元素個數(shù)為:4
                       刪除值為abc后的元素個數(shù)為:3
                       刪除自索引為1的兩個元素后的元素個數(shù):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 動態(tài)數(shù)組的使用 
{ 
 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在數(shù)組中的位置 
  Console.WriteLine("元素c在集合中的位置是:"+i); 
  i = al.LastIndexOf("d");//查找最后一個字符d在數(shù)組中的位置 
  Console.WriteLine("元素d在集合中的位置是:" + i); 
  int j = al.BinarySearch("f");//查找元素f在數(shù)組中的位置 
  if (j>0) 
  { 
  Console.WriteLine("元素f在數(shù)組中的位置是:"+j); 
  } 
  else 
  { 
  Console.WriteLine("沒有找到a"); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</span> 

輸出的結果為:元素c在集合中的位置是:2
                       元素d在集合中的位置是:3
                       元素f在數(shù)組中的位置是:5
四、ArrayList元素的遍歷
在執(zhí)行上述程序的過程中已經使用foreach語句進行ArrayList元素的遍歷,在這里就不再舉例進行說明。

以上就是關于C#的ArrayList類的相關介紹,希望對大家的學習有所幫助。

上一篇:輕松學習C#的密封類

欄    目:C#教程

下一篇:C#實現(xiàn)窗體間傳值實例分析

本文標題:輕松學習C#的ArrayList類

本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6826.html

網(wǎng)頁制作CMS教程網(wǎng)絡編程軟件編程腳本語言數(shù)據(jù)庫服務器

如果侵犯了您的權利,請與我們聯(lián)系,我們將在24小時內進行處理、任何非本站因素導致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權所有