集合 ArrayList

1.使用集合:

添加三个类Animal.cs  Cow.cs  Chiken.cs

复制代码
Animal.cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace collection
 7 {
 8     public abstract class Animal
 9     {
10         protected string name;
11 
12         public string Name
13         {
14             get { return name; }
15             set { name = value; }
16         }
17 
18         public Animal()
19         {
20             name = "The animal with no name";
21         }
22         public Animal(string newName)
23         {
24             name = newName;
25         }
26 
27         public void Feed()
28         {
29             Console.WriteLine("{0} has been feed.", name);
30         }
31     }
32 }
复制代码
复制代码
Cow.cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace collection
 7 {
 8     class Cow : Animal
 9     {
10         public void Milk()
11         {
12             Console.WriteLine("{0} has been milkd", name);
13         }
14 
15         public Cow(string newName)
16             : base(newName)
17         {
18         }
19     }
20 }
复制代码
复制代码
Chiken.cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace collection
 7 {
 8     public class Chiken:Animal
 9     {
10         public void LayEgg()
11         {
12             Console.WriteLine("{0} has laid an egg.",name);
13         }
14 
15         public Chiken(string newName):base(newName)
16         { }
17     }
18 }
复制代码

输出端

复制代码
Program.cs
 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace collection
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("创造一个Array类型收集的动物 " +
14                 "对象并使用它:");
15             Animal[] animalArray = new Animal[2];
16             Cow myCow1 = new Cow("Deirdre");
17             animalArray[0] = myCow1;
18             animalArray[1] = new Chiken("Ken");
19 
20             foreach (Animal myAnimal in animalArray)
21             {
22                 Console.WriteLine("New {0} object added to Array collection, " +
23                     "Name = {1}", myAnimal.ToString(), myAnimal.Name);
24             }
25             //Array.Length
26             Console.WriteLine("Array collection contains {0} objects.", animalArray.Length);
27             //Array调用方法时,类型转换
28             animalArray[0].Feed();
29             ((Chiken)animalArray[1]).LayEgg();
30             Console.WriteLine("===================");
31 
32             Console.WriteLine("创造一个ArrayList类型收集的动物" +
33                 "对象并使用它:");
34 
35             ArrayList animalArrayList = new ArrayList();
36             Cow myCow2 = new Cow("Hayley");
37             animalArrayList.Add(myCow2);
38             animalArrayList.Add(new Chiken("Roy"));
39 
40             foreach (Animal myAnimal in animalArrayList)
41             {
42                 Console.WriteLine("New {0} object added to ArrayList collection," +
43                     "Name = {1}", myAnimal.ToString(), myAnimal.Name);
44             }
45             //ArrayList.Count
46             Console.WriteLine("ArrayList collection contains {0} objects.", animalArrayList.Count);
47             //ArrayList调用方法时,类型转换
48             ((Animal)animalArrayList[0]).Feed();
49             ((Chiken)animalArrayList[1]).LayEgg();
50             Console.WriteLine("===================");
51 
52             Console.WriteLine("其他的处理ArrayList的:");
53             //RemoveAt
54             animalArrayList.RemoveAt(0);
55             ((Animal)animalArrayList[0]).Feed();
56             //AddRange
57             animalArrayList.AddRange(animalArray);
58             ((Chiken)animalArrayList[2]).LayEgg();
59             Console.WriteLine("=========animalArrayList列表内容==========");
60             foreach (Animal item in animalArrayList)
61             {
62                 Console.Write(item+" : "+item.Name+" \n" );
63             }
64             Console.WriteLine("========IndexOf===========");
65             //IndexOf
66             Console.WriteLine("The animal called {0} is at index {1}.",
67                 myCow1.Name, animalArrayList.IndexOf(myCow1));
68 
69             myCow1.Name = "Janice";
70             Console.WriteLine("The animal is now called {0}.", ((Animal)animalArrayList[1]).Name);
71 
72             Console.Read();
73         }
74     }
75 }
复制代码

 

image 

2.定义集合:

 

CollectionBase类有接口IEnumerableIListICollection

存储Animal对象的集合如下

复制代码
public class Animals:CollectionBase
    {
        public Animals()
        {

        }
        public void Add(Animal newAnimal)
        {
            List.Add(newAnimal);
        }
        public void Remove(Animal oldAnimal)
        {
            List.Remove(oldAnimal);
        }
}
复制代码

CollectionBase类可以对派生的集合使用 foreach语句,

Animals animalCollection = new Animals();
            animalCollection.Add(new Cow("Tang"));
            animalCollection.Add(new Chiken("San"));
            foreach (Animal  item in animalCollection)
            {
                
                Console.WriteLine("{0},{1}", item.ToString(), item.Name);
            }

但是不能使用

animalCollection[0].Feed();
要以这种方式实现,必须使用索引符。

3.索引符
复制代码
public Animal this[int animalIndex]
        {
            get
            {
                return (Animal)List[animalIndex];
            }
            set
            {
                List[animalIndex] = value;
            }
        }
复制代码

 

所以,在原有的3个Animal.cs,Cow.cs,Chiken.cs上,添加一个Animals.cs

复制代码
Animals.cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Collections;
 4 using System.Linq;
 5 using System.Text;
 6 
 7 namespace Ch11Ex2
 8 {
 9     public class Animals:CollectionBase
10     {
11         public Animals()
12         {
13 
14         }
15         public void Add(Animal newAnimal)
16         {
17             List.Add(newAnimal);
18         }
19         public void Remove(Animal oldAnimal)
20         {
21             List.Remove(oldAnimal);
22         }
23         public Animal this[int animalIndex]
24         {
25             get
26             {
27                 return (Animal)List[animalIndex];
28             }
29             set
30             {
31                 List[animalIndex] = value;
32             }
33         }
34     }
35 }
复制代码
复制代码
Program.cs
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Ch11Ex2
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Animals animalCollection = new Animals();
13             animalCollection.Add(new Cow("AAAAAA"));
14             animalCollection.Add(new Chiken("BBBBBB"));
15             foreach (Animal  item in animalCollection)
16             {
17                 //item.Feed();
18                 Console.WriteLine("{0},{1}", item.ToString(), item.Name);
19             }
20             //只有实现索引符(indexer)
21             //public Animal this[int animalIndex]
22             //才可以使用下面的方法
23             animalCollection[0].Feed();
24             Console.Read();
25         }
26     }
27 }
复制代码

image

作者:【唐】三三

出处:https://www.cnblogs.com/tangge/archive/2012/06/18/2553764.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   【唐】三三  阅读(227)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示