C#泛型之List操作示例

private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "";

            /*
            ArrayList AL = new ArrayList(3);
            TestData temp = new TestData(1);

            AL.Add(temp);
            AL.Add(new TestData(2));
            AL.Add(new TestData(3));

            foreach (TestData item in AL)
            {
                label1.Text += item.Val.ToString() + "   ";
                item.Val += 1;
            }

            foreach (TestData item in AL)
            {
                label1.Text += item.Val.ToString() + "   ";
            }
             */
            List<Racer> racers;
            List<Racer> racers2;
            FindRacer finder;
            //--------------------------------------------------------------------------------
            //生成泛型对象
            racers = new List<Racer>();
            racers.Add(new Racer("M J", "L"));
            racers.Add(new Racer("AD3 R", "OK3"));
            racers.Add(new Racer("M2 O", "L2"));
            racers.Add(new Racer("AD2 Q", "OK2"));
            racers.Add(new Racer("M1 W", "L"));
            racers.Add(new Racer("AD4 A", "OK4"));
            racers.Add(new Racer("AD1 L", "OK1"));
           
            //跌代显示泛型对象所有元素
            label1.Text += "\n跌代显示泛型对象所有元素\n";
            foreach (Racer item in racers)
            {
                label1.Text += item.ToString() + "   ";
            }
            //--------------------------------------------------------------------------------
            /*
            List<T>查找对象:Preadcate<T>是一个委托,它引用一个谓词方法.
            public T Find<Predicate<T> match);
            public List<T> FindAll<Predicate<T> match);
            */
            //委托查找所有满足条件的元素
            label1.Text += "\nFindAll(Car==\"L\")\n";
            finder = new FindRacer("L");
            foreach(Racer item in racers.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))    
            {
                label1.Text += item.ToString() + "   ";
            }
            label1.Text += "\nFindAll(Car==\"L\")将结果赋给其它泛型对象\n";
            racers2 = racers.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate));//返回泛型对象
            foreach (Racer item in racers2)
            {
                label1.Text += item.ToString() + "   ";
            }

            //委托查找一个元素(找到的第一个元素)
            label1.Text += "\nFind(Car==\"L\")\n";
            finder = new FindRacer("L2");
            label1.Text += racers.Find(new Predicate<Racer>(finder.DrivingCarPredicate)).ToString();

            //将查找出的元素做修改
            racers.Find(new Predicate<Racer>(finder.DrivingCarPredicate)).Car = "L";

            label1.Text += "\n将查找出元素修改过后\n";
            finder = new FindRacer("L");
            foreach (Racer item in racers.FindAll(new Predicate<Racer>(finder.DrivingCarPredicate)))
            {
                label1.Text += item.ToString() + "   ";
            }
            //--------------------------------------------------------------------------------
            /*
            List<T>.ForEach()可以用于对每个元素执行一些操作.
            它使用Action<T>委托对集合中的每个元素执行操作.Action<T>委托返回的类型是void.
            */
            racers.ForEach(delegate(Racer r) { r.Car += "_CAR"; });
            label1.Text += "\nList<T>.ForEach()全部修改对象\n";
            foreach (Racer item in racers)
            {
                label1.Text += item.ToString() + "   ";
            }
            //--------------------------------------------------------------------------------
            /*
            List<T>.Sort对元素进行排序
            */
            //public void List<T>.Sort(Comparison<T>), 是一个委托方法,该方法带两个T类型参数;
            racers.Sort(delegate(Racer r1, Racer r2) { return r1.Car.CompareTo(r2.Car); });
            label1.Text += "\n 排序public void List<T>.Sort(Comparison<T>)\n";
            foreach (Racer item in racers)
            {
                label1.Text += item.ToString() + "   ";
            }
            //public void List<T>.Sort(IComparer<T>)
            racers.Sort(new RacerComparer(RacerComparer.CompareType.Name));
            label1.Text += "\n 排序public void List<T>.Sort(IComparer<T>)\n";
            foreach (Racer item in racers)
            {
                label1.Text += item.ToString() + "   ";
            }
            //--------------------------------------------------------------------------------
            /*
            类型转换
             */
            List<Person> persons = racers.ConvertAll<Person>(delegate(Racer r)
            {
                int ixSeparator = r.Name.LastIndexOf(' ') + 1;
                string lastname = r.Name.Substring(ixSeparator, r.Name.Length - ixSeparator);
                string firstname = r.Name.Substring(0, ixSeparator - 1);
                return(new Person(firstname, lastname));
            });
            label1.Text += "\n List<T>.ConvertAll类型转换\n";
            foreach (Person item in persons)
            {
                label1.Text += item.ToString() + "   ";
            }
            label1.Text += "\n 类型转换后的源数据\n";
            foreach (Racer item in racers)
            {
                label1.Text += item.ToString() + "   ";
            }
            //--------------------------------------------------------------------------------
        }


//--------------------------------------------------------------------------------
    //实体类
    class Racer
    {
        private string name;
        public string Name
        {
            get
            {
                return (name);
            }
        }

        private string car;
        public string Car
        {
            get
            {
                return (car);
            }
            set
            {
                car = value;
            }
        }

        public Racer(string name, string car)
        {
            this.name = name;
            this.car = car;
        }

        public override string ToString()
        {
           return name + "," + car;
        }
    }
    //--------------------------------------------------------------------------------
    //查找类
    class FindRacer
    {
        private string car;

        public FindRacer(string car)
        {
            this.car = car;
        }

        public bool DrivingCarPredicate(Racer racer)
        {
            return (racer.Car == car);
        }
    }
    //排序类
    class RacerComparer : IComparer<Racer>
    {
        public enum CompareType
        {
            Name, Car
        }

        private CompareType compareType;
        public RacerComparer(CompareType compareType)
        {
            this.compareType = compareType;
        }

        public int Compare(Racer x, Racer y)
        {
            int result = 0;
            switch (compareType)
            {
                case CompareType.Name:
                    {
                        result = x.Name.CompareTo(y.Name);
                        break;
                    }
                case CompareType.Car:
                    {
                        result = x.Car.CompareTo(x.Car);
                        break;
                    }
            }
            return (result);
        }
    }
    //--------------------------------------------------------------------------------
    //类型转换类
    public class Person
    {
        private string firstname;
        private string lastname;

        public Person(string firstname, string lastname)
        {
            this.firstname = firstname;
            this.lastname = lastname;
        }

        public override string ToString()
        {
            return (firstname + "." + lastname);
        }
    }

posted on 2008-10-25 22:56  草原和大树  阅读(924)  评论(0编辑  收藏  举报