C#泛型之List操作示例
转自:http://blog.itpub.net/12639172/viewspace-478294/
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 racers;
            List racers2;
            FindRacer finder;
            //--------------------------------------------------------------------------------
            //生成泛型对象
            racers = new List();
            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查找对象:Preadcate是一个委托,它引用一个谓词方法.
            public T Find match);
            public List FindAll match);
            */
            //委托查找所有满足条件的元素
            label1.Text += "\nFindAll(Car==\"L\")\n";
            finder = new FindRacer("L");
            foreach(Racer item in racers.FindAll(new Predicate(finder.DrivingCarPredicate)))     
            {
                label1.Text += item.ToString() + "   ";
            }
            label1.Text += "\nFindAll(Car==\"L\")将结果赋给其它泛型对象\n";
            racers2 = racers.FindAll(new Predicate(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(finder.DrivingCarPredicate)).ToString();
            //将查找出的元素做修改
            racers.Find(new Predicate(finder.DrivingCarPredicate)).Car = "L";
            label1.Text += "\n将查找出元素修改过后\n";
            finder = new FindRacer("L");
            foreach (Racer item in racers.FindAll(new Predicate(finder.DrivingCarPredicate)))
            {
                label1.Text += item.ToString() + "   ";
            }
            //--------------------------------------------------------------------------------
            /*
            List.ForEach()可以用于对每个元素执行一些操作.
            它使用Action委托对集合中的每个元素执行操作.Action委托返回的类型是void.
            */
            racers.ForEach(delegate(Racer r) { r.Car += "_CAR"; });
            label1.Text += "\nList.ForEach()全部修改对象\n";
            foreach (Racer item in racers)
            {
                label1.Text += item.ToString() + "   ";
            }
            //--------------------------------------------------------------------------------
            /*
            List.Sort对元素进行排序
            */
            //public void List.Sort(Comparison), 是一个委托方法,该方法带两个T类型参数;
            racers.Sort(delegate(Racer r1, Racer r2) { return r1.Car.CompareTo(r2.Car); });
            label1.Text += "\n 排序public void List.Sort(Comparison)\n";
            foreach (Racer item in racers)
            {
                label1.Text += item.ToString() + "   ";
            }
            //public void List.Sort(IComparer)
            racers.Sort(new RacerComparer(RacerComparer.CompareType.Name));
            label1.Text += "\n 排序public void List.Sort(IComparer)\n";
            foreach (Racer item in racers)
            {
                label1.Text += item.ToString() + "   ";
            }
            //--------------------------------------------------------------------------------
            /*
            类型转换
             */
            List persons = racers.ConvertAll(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.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
    {
        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);
        }
    }

阅读更多
个人分类: C#
想对作者说点什么? 我来说一句

C# List使用总结

2016年02月03日 111KB 下载

没有更多推荐了,返回首页

不良信息举报

C#泛型之List操作示例

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭