集合 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

posted @ 2012-06-18 16:43  【唐】三三  阅读(223)  评论(0编辑  收藏  举报