System Array和 System Co| lections. ArrayList的使用举例 WriteLine( Additional manipulation of ArrayList malArrayList. RemoveAt(O) ((Animal)animalArrayList [o]). FeedO animalArrayList. AddRange (animalArray ((Chicken) animalArrayList [2]). LayEggo) Console. WriteLine($"The animal called my Cowl. Name] is at index fanimalArrayList. IndexOf(myCow1)]. " myCowl. Name ="Mary WriteLine("The animal is now called [((Animal)animalArrayList [1]). Name]. " ReadEy 注: Arraylist集合类可以用 AddRange o方法和 nsertRange0方法一次 添加好多项,参数可以是带有 Collect ion接口的任意对象,包括数组 这两个方法是专用于 Arraylist类的,不是任何接口的一部分
System.Array和System.Collections.ArrayList的使用举例 注:ArrayList集合类可以用AddRange()方法和InsertRange()方法一次 添加好多项,参数可以是带有Icollection接口的任意对象,包括数组。 这两个方法是专用于ArrayList类的,不是任何接口的一部分
System Array和 System Co| lections. ArrayList的使用举例 Create an Array type collection of Animal objects and use it: New Ch11Ex01 Cow object added to Array collection, Name Lea New Chi1Ex01 Chicken object added to Array collection, Name =Noa Array collection contains 2 objects Lea has been fed Noa has laid an egg Create an ArrayList type collection of Animal objects and use it: New Ch11Ex01 Cow object added to ArrayList collection, Name=Rual New Ch11Ex01 Chicken object added to ArrayList collection, Name =Andrea ArrayList collection contains 2 objects Rual has been fed Andrea has laid an egg Additional manipulation of Arraylist: Andrea has been fed Noa has laid an egg. The animal called Lea is at index 1 The animal is now called <<(Animal)an imalArrayList[11>Name)
System.Array和System.Collections.ArrayList的使用举例
212定义集 定义集合类可以从一个类中派生自己的集合,例如 System. Co lections. Collect ion Base类,这个抽象类提 供了集合类的大量实现代码。 ■ Collection Base类继承了接口 IList、 I Co ection、 lEnumer ab|e,但只提供了一些必要的实现代码,主要有 list的 Clear0)和 RemoveAt o方法, Icol lection的 Count属性。如果要使用更多功能,需要自己实现代码 ■ Collection Base类中提供了两个受保护的属性List和 I nnerList,List是list接口类型, I nnerList是 Arraylist类型
2.1.2 定义集合 定义集合类可以从一个类中派生自己的集合,例如 System.Collections.CollectionBase类,这个抽象类提 供了集合类的大量实现代码。 CollectionBase类继承了接口IList、ICollection、 IEnumerable,但只提供了一些必要的实现代码,主要有 Ilist的Clear()和RemoveAt()方法,Icollection的 Count属性。如果要使用更多功能,需要自己实现代码。 CollectionBase类中提供了两个受保护的属性List和 InnerList,List是IList接口类型,InnerList是 ArrayList类型
21.5键控集合和 IDictionar ■ Collect ionBase类的属性和方法 主要属性 > Count获取包含在 Co lection Base实例中的元素数。 I netlIst获取一个 ArrayList,它包含 CollectionBase 实例中元素的列表。 List获取一个‖List,它包含 Col lect ion Base实例中元 素的列表。 主要方法: lear0)清除 Dict ionary Base实例的内容
2.1.5 键控集合和IDictionary CollectionBase类的属性和方法 主要属性 : Count 获取包含在 CollectionBase 实例中的元素数。 InnerList 获取一个 ArrayList,它包含 CollectionBase 实例中元素的列表。 List 获取一个 IList,它包含 CollectionBase 实例中元 素的列表。 主要方法: Clear() 清除 DictionaryBase 实例的内容
212定义集 ■定义集合类 An ima|s public class Animals: CollectionBase public void Add(Animal newAnimal) List. Add(newAnimal) public void Remove (Animal newAnimal List. Remove (newAnimal)
2.1.2 定义集合 定义集合类Animals