swollaws
漂泊中体会不到生活的味道,那是因为吃不到老妈烧的饭。

      System.Collections.Generic命名空间中的List<T>类的用法非常类似于System.Collections命名空间中的ArrayList类。这个类实现了IList、ICollection和IEnumerable接口。下面针对实例来说明如果使用List<T>类。

  1. //源代码下载路径:http://media.wiley.com/product_ancillary/41/07645753/DOWNLOAD/Ch10.zip
  2. namespace ListDemo
  3. {
  4.     using System;
  5.     using System.Collections.Generic;
  6.  
  7.     public class Racer
  8.     {
  9.         private string name;
  10.         public string Name
  11.         {
  12.             get { return name; }
  13.         }
  14.  
  15.         private string car;
  16.         public string Car
  17.         {
  18.             get { return car; }
  19.         }
  20.  
  21.         public Racer(string name, string car)
  22.         {
  23.             this.name = name;
  24.             this.car = car;
  25.         }
  26.  
  27.         public override string ToString()
  28.         {
  29.             return name;
  30.         }
  31.     }
  32.  
  33.     public class Person
  34.     {
  35.         private string firstname;
  36.         public string Firstname
  37.         {
  38.             get { return firstname; }
  39.             set { firstname = value; }
  40.         }
  41.  
  42.         private string lastname;
  43.         public string Lastname
  44.         {
  45.             get { return lastname; }
  46.             set { lastname = value; }
  47.         }
  48.  
  49.         public Person(string firstname, string lastname)
  50.         {
  51.             this.firstname = firstname;
  52.             this.lastname = lastname;
  53.         }
  54.  
  55.         public override string ToString()
  56.         {
  57.             return firstname + " " + lastname;
  58.         }
  59.     }
  60.  
  61.     public class FindRacer
  62.     {
  63.         private string car;
  64.         public FindRacer(string car)
  65.         {
  66.             this.car = car;
  67.         }
  68.  
  69.         /// <summary>
  70.         /// 谓词方法--Predicate<T>泛型委托调用的方法
  71.         /// public delegate bool Predicate<T>(T obj)--委托定义
  72.         /// </summary>
  73.         public bool DrivingCarPredicate(Racer racer)
  74.         {
  75.             return racer.Car == car;
  76.         }
  77.     }
  78.  
  79.     public class RacerComparer : IComparer<Racer>
  80.     {
  81.         public enum CompareType
  82.         {
  83.             Name,
  84.             Car
  85.         }
  86.  
  87.         private CompareType compareType;
  88.  
  89.         public RacerComparer(CompareType compareType)
  90.         {
  91.             this.compareType = compareType;
  92.         }
  93.  
  94.         #region IComparer<Racer> Members
  95.         public int Compare(Racer x, Racer y)
  96.         {
  97.             int result = 0;
  98.             switch (compareType)
  99.             {
  100.                 case CompareType.Name:
  101.                     result = x.Name.CompareTo(y.Name);
  102.                     break;
  103.                 case CompareType.Car:
  104.                     result = x.Car.CompareTo(y.Car);
  105.                     break;
  106.             }
  107.             return result;
  108.         }
  109.         #endregion
  110.     }
  111.  
  112.     class Program
  113.     {
  114.         static void Main(string[] args)
  115.         {
  116.             //初始化
  117.             List<Racer> list = new List<Racer>();                  
  118.             //添加对象--这里只能使用Racer类的实例,体现的就是泛型的类型安全
  119.             list.Add(new Racer("Michael Schumacher", "Ferrari"));  
  120.             list.Add(new Racer("Juan Pablo Montoya", "McLaren-Mercedes"));
  121.             list.Add(new Racer("Kimi Raikkonen", "McLaren-Mercedes"));
  122.             list.Add(new Racer("Mark Webber", "Williams-BMW"));
  123.             list.Add(new Racer("Rubens Barichello", "Ferrari"));
  124.  
  125.             #region 1、查找元素--FindAll(Predicate<T> match)|执行操作ForEach(Action<T> action)
  126.             FindRacer finder = new FindRacer("Ferrari");
  127.             //foreach (Racer racer in list.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))
  128.             //{
  129.             //    Console.WriteLine(racer);
  130.             //}//和下面的语句执行相同
  131.             list.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate))
  132.                 .ForEach(delegate(Racer r) { Console.WriteLine(r); });//ForEach方法中使用匿名委托
  133.  
  134.             //反编译后的FindAll方法
  135.             //public List<T> FindAll(Predicate<T> match)
  136.             //{
  137.             //    if (match == null)
  138.             //    {
  139.             //        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  140.             //    }
  141.             //    List<T> list = new List<T>();
  142.             //    for (int i = 0; i < this._size; i++)
  143.             //    {
  144.             //        if (match(this._items[i]))
  145.             //        {
  146.             //            list.Add(this._items[i]);
  147.             //        }
  148.             //    }
  149.             //    return list;
  150.             //}
  151.             //反编译后的ForEach方法
  152.             //public void ForEach(Action<T> action)
  153.             //{
  154.             //    if (action == null)
  155.             //    {
  156.             //        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  157.             //    }
  158.             //    for (int i = 0; i < this._size; i++)
  159.             //    {
  160.             //        action(this._items[i]);
  161.             //    }
  162.             //}
  163.             #endregion
  164.  
  165.             #region 2、对元素排序Sort(IComparer<T>)|Sort(Comparison<T>)
  166.             //使用指定的Comparison<T>【委托】对整个List<T> 中的元素进行排序--这里是匿名委托--返回整数
  167.             list.Sort(delegate(Racer r1, Racer r2) { return r1.Name.CompareTo(r2.Name); });
  168.             //使用指定的比较器对整个 List<T> 中的元素进行排序。
  169.             list.Sort(new RacerComparer(RacerComparer.CompareType.Car));
  170.  
  171.             Console.WriteLine(""nSorted:");
  172.             list.ForEach(delegate(Racer r) { Console.WriteLine(r); });
  173.             #endregion
  174.  
  175.             #region 3、类型转换
  176.             List<Person> persons = list.ConvertAll<Person>(delegate(Racer r)
  177.             {
  178.                 int ixSeperator = r.Name.LastIndexOf(' ') + 1;
  179.                 string lastname = r.Name.Substring(ixSeperator, r.Name.Length - ixSeperator);
  180.                 string firstname = r.Name.Substring(0, ixSeperator - 1);
  181.                 return new Person(firstname, lastname);
  182.             });
  183.  
  184.             Console.WriteLine(""nPersons:");
  185.             persons.ForEach(delegate(Person p) { Console.WriteLine(p); });
  186.             #endregion
  187.  
  188.             Console.ReadLine();
  189.         }
  190.     }
  191. }
posted on 2009-05-12 11:25  swollaw  阅读(593)  评论(0编辑  收藏  举报