吉吉于

List ,Stack ,Queue ,SortedSet

几个比较有用的泛型类。

 

01 6using System;
02 using System.Collections.Generic;
03 using System.Linq;
04 using System.Text;
05 using System.Threading.Tasks;
06
07 namespace Lazynight
08 {
09     class Program
10     {
11         static void Main(string[] args)
12         {
13             UseGenericList();
14             Console.ReadLine();
15         }
16         private static void UseGenericList()
17         {   //List</span>
18             //动态调整内容
19             /*List people = new List() </span>
20             {
21                 new Person(“Homer”,”Simpon”,14),
22                 new Person(“Lazy”,”Night”,16)
23             };
24             Console.WriteLine(“Items in list:{0}”,people.Count);
25             foreach(Person p in people)
26                 Console.WriteLine(p);
27             Console.WriteLine(“Inserting new person.”);
28             people.Insert(1,new Person(“Flower”,”Owl”,18));
29             Console.WriteLine(“Items in list:{0}”,people.Count);
30             Person[] arrayOfPeople = people.ToArray();
31             for (int i = 0; i < arrayOfPeople.Length; i++)
32             {
33                 Console.WriteLine(“First Names:{0}”,arrayOfPeople[i].FirstName);
34             }*/
35             //Stack</span>
36             //后进先出维护数据集合
37             /*
38             Stack stackOfPeople = new Stack();</span>
39             stackOfPeople.Push(new Person(“Homer”, “Simpon”, 14));
40             stackOfPeople.Push(new Person(“Lazy”, “Night”, 15));
41             Console.WriteLine(“First person is:{0}”,stackOfPeople.Peek());
42             Console.WriteLine(“Popped off {0}”,stackOfPeople.Pop());
43             Console.WriteLine(“First person is:{0}”, stackOfPeople.Peek());
44             Console.WriteLine(“Count: {0}”,stackOfPeople.Count);
45             Console.WriteLine(“Popped off {0}”, stackOfPeople.Pop());
46             Console.WriteLine(“Count: {0}”, stackOfPeople.Count);
47             */
48             //Queue</span>
49             //先进先出访问数据容器
50             /*
51             Queue peopleQ = new Queue();</span>
52             peopleQ.Enqueue(new Person(“Lazy”, “Night”, 15));
53             peopleQ.Enqueue(new Person(“Homer”, “Simpon”, 14));
54             //队列中的第一人
55             Console.WriteLine(“{0} is first in line!”,peopleQ.Peek().FirstName);
56             //移除队列中的人
57             for (int i = 0; i <= peopleQ.Count;i++ )
58                 peopleQ.Dequeue();
59             try
60             {
61                 Console.WriteLine(“{0} is first in line!”,peopleQ.Peek().FirstName);
62             }
63             catch (InvalidOperationException e)
64             {
65                 Console.WriteLine(“ERROR! {0}”,e.Message);
66             }*/
67             //SortedSet .NET4引入</span>
68             //自动确保排序正确
69             /*
70             SortedSet setOfPeople = new SortedSet(new SortPeopleByAge())</span>
71             {
72                 new Person(“Lazy”, “Night”, 15),
73                 new Person(“Homer”, “Simpon”, 14)
74             };
75             foreach(Person p in setOfPeople)
76             {
77                 Console.WriteLine(p);
78             }*/
79         }
80     }
81 } </div> 转载请注明:[于哲的博客][1] » [List ,Stack ,Queue ,SortedSet ][2] [1]: http://lazynight.me [2]: http://lazynight.me/1346.html