這篇文章給大家分享的是有關(guān)C#中集合ArrayList怎么用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)公司,專注為中小企業(yè)提供官網(wǎng)建設、營銷型網(wǎng)站制作、響應式網(wǎng)站開發(fā)、展示型成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設等服務,幫助中小企業(yè)通過網(wǎng)站體現(xiàn)價值、有效益。幫助企業(yè)快速建站、解決網(wǎng)站建設與網(wǎng)站營銷推廣問題。
具體如下:
.NET Framework提供了用于數(shù)據(jù)存儲和檢索的專用類,這些類統(tǒng)稱集合。這些類提供對堆棧、隊列、列表和哈希表的支持。大多數(shù)集合類實現(xiàn)系統(tǒng)的接口。下面我們主要來講一下ArrayList。
ArrayList是命名空間Systrm.Collections下的一部分,它是使用大小可按需動態(tài)增加的數(shù)組實現(xiàn)IList接口。
ArrayList的容量是ArrayList可以保存的元素數(shù)。ArrayList的默認初始容量為0.隨著元素添加到ArrayList中,容量會根據(jù)需要通過重新分配自動增加。使用證書索引可以訪問此集合中的元素。此集合中的索引從零開始。
下面我們先來看一個例子:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace 集合{ //動物類 public abstract class Animal { protected string name; //動物的名字 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); } } //奶牛類 public class Cow : Animal { public Cow(string newName): base(newName) { } public void Milk() //產(chǎn)牛奶 { Console.WriteLine("{0} has been milked.", name); } } //母雞類 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ù)組的方法實現(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(); //用集合的方法實現(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)建
對于簡單的數(shù)組來說,只有用固定的大小來初始化數(shù)組,才能使用它。
Animal[] animalArray = new Aimal[2];
而ArrayList集合不需要初始化其大小
ArrayList animalArrayList = new ArrayList();
這個類還有另外兩個構(gòu)造函數(shù)。第一個構(gòu)造函數(shù)把現(xiàn)有的集合作為一個參數(shù),把現(xiàn)有集合的內(nèi)容復制到新實例中;而另一個構(gòu)造函數(shù)通過一個參數(shù)設置集合的容量(capacity)。這個容量用一個int值指定,設置集合中可以包含的初始項數(shù)。但這并不是真實的容量,因為如果集合中的項數(shù)超過了這個值,容量就會自動增加一倍。
對于ArrayList集合,它沒有現(xiàn)成的項,也沒有null引用的項。我們使用ArrayList對象的Add()方法添加新項:
Cow myCow2 = new Cow("Hayley"); anmialArrayList.Add(myCow2); anmialArrayList.Add(new Chicken("Roy"));
ArrayList對象支持IEnumerable接口,這個接口的唯一方法GetEnuumerator()可以迭代集合中的各項。所以可以與foreach一起使用
foreach (Animal myAnimal in animalArrayList) { Console.WriteLine("New {0} object added to ArrayList collection,Name = {1}",myAnimal.Tostring(),myAnimal.Name); }
ArrayList集合是System.Object對象的集合(通過多態(tài)性賦給Animal對象),所以必須用所有的項進行數(shù)據(jù)類型轉(zhuǎn)換:
((Animal)animalArrayList[0]).Feed(); ((Chichen)animalArrayList[1]).LayEgg();
我們可以使用Remove()和RemoveAt()方法刪除項,這兩個方法是在ArrayList類中實現(xiàn)的IList接口的一部分。它們分別根據(jù)項的引用或索引從集合中刪除項:
animalArrayList.RemoveAt(0); animalArrayList.Remove(myCow2);
ArrayList集合可以用AddRange()方法一次添加好幾個項。這個方法接受帶有ICollection接口的任何對象,包括數(shù)組:
animalArrayList.AddRange(animalArray);
AddRange()方法不是ArrayList提供的任何接口的一部分。這個方法專用于ArrayList類
感謝各位的閱讀!關(guān)于“C#中集合ArrayList怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網(wǎng)站欄目:C#中集合ArrayList怎么用
鏈接分享:http://muchs.cn/article14/iheede.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設計、App開發(fā)、企業(yè)建站、自適應網(wǎng)站、企業(yè)網(wǎng)站制作、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)