这篇文章给大家分享的是有关C#中集合ArrayList怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
具体如下:
.NET Framework提供了用于数据存储和检索的专用类,这些类统称集合。这些类提供对堆栈、队列、列表和哈希表的支持。大多数集合类实现系统的接口。下面我们主要来讲一下ArrayList。
ArrayList是命名空间Systrm.Collections下的一部分,它是使用大小可按需动态增加的数组实现IList接口。
ArrayList的容量是ArrayList可以保存的元素数。ArrayList的默认初始容量为0.随着元素添加到ArrayList中,容量会根据需要通过重新分配自动增加。使用证书索引可以访问此集合中的元素。此集合中的索引从零开始。
下面我们先来看一个例子:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; usingSystem.Collections; namespace集合{ //动物类 pubpcabstractclassAnimal { protectedstringname;//动物的名字 pubpcstringName { get{returnname;} set{name=value;} } pubpcAnimal() { name="Theanimalwithnoname"; } pubpcAnimal(stringnewName) { name=newName; } pubpcvoidFeed()//繁殖后代 { Console.WriteLine("{0}hasbeenfed.",name); } } //奶牛类 pubpcclassCow:Animal { pubpcCow(stringnewName):base(newName) {} pubpcvoidMilk()//产牛奶 { Console.WriteLine("{0}hasbeenmilked.",name); } } //母鸡类 pubpcclassChicken:Animal { pubpcChicken(stringnewName):base(newName) {} pubpcvoidLayEgg()//下蛋 { Console.WriteLine("{0}haslaidanegg.",name); } } classProgram { staticvoidMain(string[]args) { //用数组的方法实现 Console.WriteLine("CreateanArraytypecollectionofAnimalobjectanduseit:"); Animal[]animalArray=newAnimal[2]; CowmyCow1=newCow("Deirdre"); animalArray[0]=myCow1; animalArray[1]=newChicken("Ken"); foreach(AnimalmyAnimapnanimalArray) { Console.WriteLine("New{0}objectaddedtoArraycollection,Name={1}",myAnimal.ToString(),myAnimal.Name); } Console.WriteLine("Arraycollectioncontains{0}objects.",animalArray.Length); animalArray[0].Feed(); ((Chicken)animalArray[1]).LayEgg(); Console.WriteLine(); //用集合的方法实现 Console.WriteLine("CreateanArrayListtypecollectionofAnimalobjectsanduseit;"); ArrayListanimalArrayList=newArrayList(); CowmyCow2=newCow("HayLey"); animalArrayList.Add(myCow2); animalArrayList.Add(newChicken("Roy")); foreach(AnimalmyAnimapnanimalArrayList) { Console.WriteLine("New{0}objectaddedtoArrayListcollection,Name={1}",myAnimal.ToString(),myAnimal.Name); } Console.WriteLine("ArrayListcollectioncontains{0}objects.",animalArrayList.Count); ((Animal)animalArrayList[0]).Feed(); ((Chicken)animalArrayList[1]).LayEgg(); Console.WriteLine(); } } }
集合的创建
对于简单的数组来说,只有用固定的大小来初始化数组,才能使用它。
Animal[]animalArray=newAimal[2];
而ArrayList集合不需要初始化其大小
ArrayListanimalArrayList=newArrayList();
这个类还有另外两个构造函数。第一个构造函数把现有的集合作为一个参数,把现有集合的内容复制到新实例中;而另一个构造函数通过一个参数设置集合的容量(capacity)。这个容量用一个int值指定,设置集合中可以包含的初始项数。但这并不是真实的容量,因为如果集合中的项数超过了这个值,容量就会自动增加一倍。
对于ArrayList集合,它没有现成的项,也没有null引用的项。我们使用ArrayList对象的Add()方法添加新项:
CowmyCow2=newCow("Hayley"); anmialArrayList.Add(myCow2); anmialArrayList.Add(newChicken("Roy"));
ArrayList对象支持IEnumerable接口,这个接口的唯一方法GetEnuumerator()可以迭代集合中的各项。所以可以与foreach一起使用
foreach(AnimalmyAnimapnanimalArrayList) { Console.WriteLine("New{0}objectaddedtoArrayListcollection,Name={1}",myAnimal.Tostring(),myAnimal.Name); }
ArrayList集合是System.Object对象的集合(通过多态性赋给Animal对象),所以必须用所有的项进行数据类型转换:
((Animal)animalArrayList[0]).Feed(); ((Chichen)animalArrayList[1]).LayEgg();
我们可以使用Remove()和RemoveAt()方法删除项,这两个方法是在ArrayList类中实现的IList接口的一部分。它们分别根据项的引用或索引从集合中删除项:
animalArrayList.RemoveAt(0); animalArrayList.Remove(myCow2);
ArrayList集合可以用AddRange()方法一次添加好几个项。这个方法接受带有ICollection接口的任何对象,包括数组:
animalArrayList.AddRange(animalArray);
AddRange()方法不是ArrayList提供的任何接口的一部分。这个方法专用于ArrayList类
感谢各位的阅读!关于“C#中集合ArrayList怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
以上就是C#中集合ArrayList怎么用的相关内容分享,字数约4040字,希望这对大家有所帮助!屹东网往后会继续推荐C#中集合ArrayList怎么用相关内容。