-
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());//需要某种排序,建立引入对应接口类的对象