C#中List<Type>的使用

  • List<Type>是一个集合,里面放的全是相同的Type的数据或对象。

  • 当不知道需要存放多少数据,此时用数组(数组讲解见http://t.csdn.cn/ab8hf)不太方便,new Type ary[Size]中Size的大小无法确定!此时用List<Type>集合比较方便!

以类的集合举例

先创建一个学生类

class Student 
    {
        private int age;
        public string StuName { get; set; }
        public int StuId { get; set; }
        public int Age
        {
            get => age;
            set => age = (value < 18) ? 18 : value;
        }

        public Student() { StuName = "xiexie"; StuId = 1117; Age = 23; }
        public Student(string str, int a, int i) { StuName = str; StuId = i; Age = a; }
        public string StuShow()
        {
            string info = String.Format("姓名:{0},年龄:{1},学号:{2}", StuName, Age, StuId);
            return info;
        }

    }



建立List<Student>集合

            //创建类的对象
            Student stu1 = new Student("Jack", 15, 1001);
            Student stu2 = new Student("Tom", 22, 1002);
            Student stu3 = new Student("Lucy", 35, 1003);
            Student stu4 = new Student("Eric", 8, 1004);
            
            //创建集合List<Student>的对象
            List<Student> stuList = new List<Student>();
            
            //调用add方法进行集合元素添加
            stuList.Add(stu1);
            stuList.Add(stu2);
            stuList.Add(stu3);
            stuList.Add(stu4);

            //输出集合中元素
            foreach (Student student in stuList)
            {

                Console.WriteLine(student.StuShow());
            }

            //集合内元素排序
            //stuList.Sort();此处会失效,后续讲解原因

List<Type>的排序问题

1.针对Type类型数据单一型:string, int等,直接用sort()方法,括号内不带任何参数!

2.对于type为类的对象则需要用到接口和sort()参数

对象类型举例如下:

1.针对IComparable<T>接口, 实现写好的默认排序

Student需要引入接口,如下:

    class Student : IComparable<Student>//引入默认比较排序接口
    {
        private int age;
        public string StuName { get; set; }
        public int StuId { get; set; }
        public int Age
        {
            get => age;
            set => age = (value < 18) ? 18 : value;
        }

        public Student() { StuName = "xiexie; StuId = 1117; Age = 23; }
        public Student(string str, int a, int i) { StuName = str; StuId = i; Age = a; }
        public string StuShow()
        {
            string info = String.Format("姓名:{0},年龄:{1},学号:{2}", StuName, Age, StuId);
            return info;
        }

        public int CompareTo(Student other) //默认比较接口函数
        {
            return other.StuId.CompareTo(this.StuId); //对StuId进行排序
            //通过颠倒this和other的顺序实现排序的颠倒
        }

    }

通过颠倒this和other的顺序实现排序的颠倒。

之后,Main()排序函数的使用方法如下:

//通过调用比较器对S之前tudent类中写好的默认比较函数进行排序
stuList.Sort();//IComparable<T> 实现默认排序,默认无需参数

2.针对IComparer<T> 接口,实现动态排序

Student需要引入接口如下:

    class Student : IComparer<Student>//引入动态排序接口
    {
        private int age;
        public string StuName { get; set; }
        public int StuId { get; set; }
        public int Age
        {
            get => age;
            set => age = (value < 18) ? 18 : value;
        }

        public Student() { StuName = "xiexie"; StuId = 1117; Age = 23; }
        public Student(string str, int a, int i) { StuName = str; StuId = i; Age = a; }
        public string StuShow()
        {
            string info = String.Format("姓名:{0},年龄:{1},学号:{2}", StuName, Age, StuId);
            return info;
        }

        public int Compare(Student x, Student y) //写动态排序函数
        {
            return x.StuId.CompareTo(y.StuId);
            // 通过调换x,y可以实现排序颠倒       
        }
    }

通过调换x,y可以实现排序颠倒。 

之后,Main()排序函数的使用方法如下:

    stuList.Sort(new Student());//需要引入对应接口定义的类

此处还体现不出动态排序接口的动态所在,下面见证“动态 ” 

先记住一句话:几个比较方式,定义几种类!顺序和倒序算2种  

3.动态排序详解:

(我们不难发现,Student类有好几个字段都可以进行比较,比如:StuName, Age等等)

对StuName, Age进行排序比较,需要建立四个类,如下:

class Student : IComparer<Student>
    {
        private int age;
        public string StuName { get; set; }
        public int StuId { get; set; }
        public int Age
        {
            get => age;
            set => age = (value < 18) ? 18 : value;
        }

        public Student() { StuName = "xiexie"; StuId = 1117; Age = 23; }
        public Student(string str, int a, int i) { StuName = str; StuId = i; Age = a; }
        public string StuShow()
        {
            string info = String.Format("姓名:{0},年龄:{1},学号:{2}", StuName, Age, StuId);
            return info;
        }

        public int Compare(Student x, Student y)
        {
            return x.StuId.CompareTo(y.StuId);
        }
    }


    /// <summary>
    /// 排序类:几个比较方式,定义几种类!
    /// </summary>
    class StuNameASC : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.StuName.CompareTo(y.StuName);
        }
    }

    class StuNameDESC : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return y.StuName.CompareTo(x.StuName);
        }
    }

    class AgeASC : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return x.Age.CompareTo(y.Age);
        }
    }

    class AgeDESC : IComparer<Student>
    {
        public int Compare(Student x, Student y)
        {
            return y.Age.CompareTo(x.Age);
        }
    }

之后,Main()排序函数的使用方法如下:

            stuList.Sort(new StuNameASC());//需要某种排序,建立引入对应接口类的对象

综上:动态排序体现出来了!

总结:动态排序可完全替代默认排序!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奋发秃强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值