C#入門(mén)教程之集合ArrayList用法詳解
本文實(shí)例講述了C#入門(mén)教程之集合ArrayList用法。分享給大家供大家參考,具體如下:
.NET Framework提供了用于數(shù)據(jù)存儲(chǔ)和檢索的專(zhuān)用類(lèi),這些類(lèi)統(tǒng)稱(chēng)集合。這些類(lèi)提供對(duì)堆棧、隊(duì)列、列表和哈希表的支持。大多數(shù)集合類(lèi)實(shí)現(xiàn)系統(tǒng)的接口。下面我們主要來(lái)講一下ArrayList。
ArrayList是命名空間Systrm.Collections下的一部分,它是使用大小可按需動(dòng)態(tài)增加的數(shù)組實(shí)現(xiàn)IList接口。
ArrayList的容量是ArrayList可以保存的元素?cái)?shù)。ArrayList的默認(rèn)初始容量為0.隨著元素添加到ArrayList中,容量會(huì)根據(jù)需要通過(guò)重新分配自動(dòng)增加。使用證書(shū)索引可以訪(fǎng)問(wèn)此集合中的元素。此集合中的索引從零開(kāi)始。
下面我們先來(lái)看一個(gè)例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace 集合{
//動(dòng)物類(lèi)
public abstract class Animal
{
protected string name; //動(dòng)物的名字
public string Name
{
get { return name; }
set { name = value; }
}
public Animal()
{
name="The animal with no name";
}
public Animal(string newName)
{
name=newName;
}
public void Feed() //繁殖后代
{
Console.WriteLine("{0} has been fed.",name);
}
}
//奶牛類(lèi)
public class Cow : Animal
{
public Cow(string newName): base(newName)
{ }
public void Milk() //產(chǎn)牛奶
{
Console.WriteLine("{0} has been milked.", name);
}
}
//母雞類(lèi)
public class Chicken : Animal
{
public Chicken(string newName):base(newName)
{ }
public void LayEgg() //下蛋
{
Console.WriteLine("{0} has laid an egg.", name);
}
}
class Program
{
static void Main(string[] args)
{
//用數(shù)組的方法實(shí)現(xiàn)
Console.WriteLine("Create an Array type collection of Animal object and use it:");
Animal[] animalArray = new Animal[2];
Cow myCow1 = new Cow("Deirdre");
animalArray[0] = myCow1;
animalArray[1] = new Chicken("Ken");
foreach (Animal myAnimal in animalArray )
{
Console.WriteLine("New {0} object added to Array collection,Name={1}", myAnimal.ToString(), myAnimal.Name);
}
Console.WriteLine("Array collection contains {0} objects.",animalArray .Length );
animalArray[0].Feed();
((Chicken)animalArray[1]).LayEgg();
Console.WriteLine();
//用集合的方法實(shí)現(xiàn)
Console.WriteLine("Create an ArrayList type collection of Animal objects and use it;");
ArrayList animalArrayList = new ArrayList();
Cow myCow2 = new Cow("HayLey");
animalArrayList.Add(myCow2);
animalArrayList.Add(new Chicken("Roy"));
foreach (Animal myAnimal in animalArrayList)
{
Console.WriteLine("New {0} object added to ArrayList collection,Name={1}", myAnimal.ToString(), myAnimal.Name);
}
Console.WriteLine("ArrayList collection contains {0} objects.", animalArrayList.Count);
((Animal)animalArrayList[0]).Feed();
((Chicken)animalArrayList[1]).LayEgg();
Console.WriteLine();
}
}
}

集合的創(chuàng)建
對(duì)于簡(jiǎn)單的數(shù)組來(lái)說(shuō),只有用固定的大小來(lái)初始化數(shù)組,才能使用它。
Animal[] animalArray = new Aimal[2];
而ArrayList集合不需要初始化其大小
ArrayList animalArrayList = new ArrayList();
這個(gè)類(lèi)還有另外兩個(gè)構(gòu)造函數(shù)。第一個(gè)構(gòu)造函數(shù)把現(xiàn)有的集合作為一個(gè)參數(shù),把現(xiàn)有集合的內(nèi)容復(fù)制到新實(shí)例中;而另一個(gè)構(gòu)造函數(shù)通過(guò)一個(gè)參數(shù)設(shè)置集合的容量(capacity)。這個(gè)容量用一個(gè)int值指定,設(shè)置集合中可以包含的初始項(xiàng)數(shù)。但這并不是真實(shí)的容量,因?yàn)槿绻现械捻?xiàng)數(shù)超過(guò)了這個(gè)值,容量就會(huì)自動(dòng)增加一倍。
對(duì)于ArrayList集合,它沒(méi)有現(xiàn)成的項(xiàng),也沒(méi)有null引用的項(xiàng)。我們使用ArrayList對(duì)象的Add()方法添加新項(xiàng):
Cow myCow2 = new Cow("Hayley");
anmialArrayList.Add(myCow2);
anmialArrayList.Add(new Chicken("Roy"));
ArrayList對(duì)象支持IEnumerable接口,這個(gè)接口的唯一方法GetEnuumerator()可以迭代集合中的各項(xiàng)。所以可以與foreach一起使用
foreach (Animal myAnimal in animalArrayList)
{
Console.WriteLine("New {0} object added to ArrayList collection,Name = {1}",myAnimal.Tostring(),myAnimal.Name);
}
ArrayList集合是System.Object對(duì)象的集合(通過(guò)多態(tài)性賦給Animal對(duì)象),所以必須用所有的項(xiàng)進(jìn)行數(shù)據(jù)類(lèi)型轉(zhuǎn)換:
((Animal)animalArrayList[0]).Feed(); ((Chichen)animalArrayList[1]).LayEgg();
我們可以使用Remove()和RemoveAt()方法刪除項(xiàng),這兩個(gè)方法是在ArrayList類(lèi)中實(shí)現(xiàn)的IList接口的一部分。它們分別根據(jù)項(xiàng)的引用或索引從集合中刪除項(xiàng):
animalArrayList.RemoveAt(0); animalArrayList.Remove(myCow2);
ArrayList集合可以用AddRange()方法一次添加好幾個(gè)項(xiàng)。這個(gè)方法接受帶有ICollection接口的任何對(duì)象,包括數(shù)組:
animalArrayList.AddRange(animalArray);
AddRange()方法不是ArrayList提供的任何接口的一部分。這個(gè)方法專(zhuān)用于ArrayList類(lèi),
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《C#字符串操作技巧總結(jié)》、《C#數(shù)組操作技巧總結(jié)》、《C#中XML文件操作技巧匯總》、《C#常見(jiàn)控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
分享WCF聊天程序--WCFChat實(shí)現(xiàn)代碼
無(wú)意中在一個(gè)國(guó)外的站點(diǎn)下到了一個(gè)利用WCF實(shí)現(xiàn)聊天的程序,作者是:Nikola Paljetak。研究了一下,自己做了測(cè)試和部分修改,感覺(jué)還不錯(cuò),分享給大家2015-11-11
C#實(shí)現(xiàn)Windows服務(wù)安裝卸載開(kāi)啟停止
本文主要介紹了C#實(shí)現(xiàn)Windows服務(wù)安裝卸載開(kāi)啟停止,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
C#中使用Interlocked進(jìn)行原子操作的技巧
使用.NET提供的Interlocked類(lèi)可以對(duì)一些數(shù)據(jù)進(jìn)行原子操作,看起來(lái)似乎跟lock鎖一樣,但它并不是lock鎖,它的原子操作是基于CPU本身的,非阻塞的,所以要比lock的效率高2016-12-12
C#中Thread(線(xiàn)程)和Task(任務(wù))實(shí)例詳解
.NET Framework在System.Threading命名空間中具有與線(xiàn)程相關(guān)的類(lèi),線(xiàn)程是一小組可執(zhí)行指令,這篇文章主要給大家介紹了關(guān)于C#中Thread(線(xiàn)程)和Task(任務(wù))的相關(guān)資料,需要的朋友可以參考下2022-03-03
詳解C#設(shè)置Excel數(shù)據(jù)自適應(yīng)行高、列寬的2種情況
這篇文章主要介紹了C#設(shè)置Excel數(shù)據(jù)自適應(yīng)行高、列寬的2種情況,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

