輕松學習C#的ArrayList類
動態(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#的ArrayList類
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6826.html
您可能感興趣的文章
- 01-10C#通過反射獲取當前工程中所有窗體并打開的方法
- 01-10關于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#停止線程的方法
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實現(xiàn)多線程下載文件的方法


閱讀排行
本欄相關
- 01-10C#通過反射獲取當前工程中所有窗體并
- 01-10關于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實現(xiàn)txt定位指定行完整實例
- 01-10WinForm實現(xiàn)仿視頻播放器左下角滾動新
- 01-10C#停止線程的方法
- 01-10C#實現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實現(xiàn)讀取注冊表監(jiān)控當前操作系統(tǒng)已
隨機閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10C#中split用法實例總結
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢data目錄下的sessions文件夾有什


