文章目录
- 1 List泛型集合的使用
- 1.1 List\简要介绍
- 1.2 List\的创建
- 1.3 List\和Array的互相转换
- 1.4 List\删除元素
- 1.5 List\的遍历
- 1.6 List\的快速查询
- 2 List泛型集合的排序
- 2.1 值类型元素的排序
- 2.2 类类型元素使用默认比较器进行排序
- 2.3 类类型元素使用比较器接口进行排序
- 2.4 其他高级排序方法
- 3 泛型集合List作为DataGridView数据源展示和动态排序实现
- 3.1 项目文件和界面UI
- 3.2 代码实现
1 List泛型集合的使用
1.1 List<T>简要介绍
对于集合:定义的时候,无需规定元素的个数。
泛型:表示一种程序特性,也就是我们在定义的时候,无需指定特定的类型,而在使用的时候,我们必须明确类型。可以应用在集合中、方法中、类中。表示为<T>。<T>表示泛型,T是Type的简写,表示当前不确定具体类型。可以根据用户的实际需要,确定当前集合需要存放的数据类型,一旦确定不可改变。
要求:添加到集合中的元素类型,必须和泛型集合定义时规定的数据类型完全一致。数据取出后无需强制转换。
使用前的准备:
- 引入命名空间:System.Collections.Generic。
- 确定存储类型:List students = new List();
1.2 List<T>的创建
- 使用add方法依次添加。
List<Course> courseList = new List<Course>();
courseList.Add(course1);
courseList.Add(course2);
courseList.Add(course3);
courseList.Add(course4);
courseList.Add(course5);
- 使用集合初始化器,将元素一次性的初始化到集合中。
List<Course> courseList = new List<Course>() { course1, course2, course3, course4, course5 };
// Course[] courseArray1 = new Course[] { course1, course2, course3, course4, course5 };
- 把数组中的元素添加到集合中。
List<Course> courseListFromArray = new List<Course>();
courseListFromArray.AddRange(courseArray1);
1.3 List<T>和Array的互相转换
//集合转换到数组
Course[] courseArray2 = courseList.ToArray();
//数组直接转换到集合
List<Course> courseList3 = courseArray2.ToList();
1.4 List<T>删除元素
courseList.Remove(course3);//必须要掌握
courseList.RemoveAll(c => c.CourseId > 10002);//了解
courseList.RemoveAt(2);
courseList.RemoveRange(1, 2);
1.5 List<T>的遍历
- 使用for循环。
for (int i = 0; i < courseList.Count; i++)
{
Console.WriteLine($"{item.CourseId}\t{item.CourseName}\t{item.ClassHour}\t{item.Teacher}");
}
- 使用foreach。
foreach (Course item in courseList)
{
Console.WriteLine($"{item.CourseId}\t{item.CourseName}\t{item.ClassHour}\t{item.Teacher}");
}
1.6 List<T>的快速查询
集合查询方法1:
List<Course> result1 = courseList.FindAll(c => c.CourseId > 10003);
集合查询方法2:
var result2 = from c in courseList where c.CourseId > 10003 select c;
var result3 = result2.ToList();
2 List泛型集合的排序
2.1 值类型元素的排序
List<int> ageList = new List<int> { 20, 19, 25, 30, 26 };
ageList.Sort();//默认按照升序排列
foreach (int item in ageList)
{
Console.WriteLine(item);
}
ageList.Reverse(); //降序
foreach (int item in ageList)
{
Console.WriteLine(item);
}
2.2 类类型元素使用默认比较器进行排序
我们对需要排序的类实现系统接口IComparable<Course>。
public class Course : IComparable<Course>
{
public Course() { }
public Course(int courseId, string courseName, int classHour, string teacher)
{
this.CourseId = courseId;
this.CourseName = courseName;
this.ClassHour = classHour;
this.Teacher = teacher;
}
public int CourseId { get; set; }//课程编号
public string CourseName { get; set; }//课程名称
public int ClassHour { get; set; }//课时
public string Teacher { get; set; }//主讲老师
//接口对应的比较方法(这个方法的签名,千万不要动)
public int CompareTo(Course other)
{
//return this.CourseId.CompareTo(other.CourseId);
return other.CourseId.CompareTo(CourseId);
//如果把this放到前面,表示按照升序排列,other放到前面就是按照降序排列
}
}
2.3 类类型元素使用比较器接口进行排序
以上我们使用默认比较器进行排序,很不方便,如果我们需要多种排序,怎么办?比较器接口:其实就是我们可以任意的指定对象属性排序,从而实现动态排序。
/// <summary>
/// 课程编号升序
/// </summary>
class CourseIdASC : IComparer<Course>
{
public int Compare(Course x, Course y)
{
return x.CourseId.CompareTo(y.CourseId);
}
}
// 使用方式
//排序方法的定义: public void Sort(IComparer<T> comparer);
courseList.Sort(new CourseIdASC());
2.4 其他高级排序方法
// 使用LINQ实现排序
var list1 = from c in courseList orderby c.CourseId ascending select c;
// 使用扩展方法OrderByDescending实现降序
var list2 = courseList.OrderByDescending(c => c.CourseId);
// 使用扩展方法OrderBy实现升序
var list3 = courseList.OrderBy(c => c.ClassHour);
3 泛型集合List作为DataGridView数据源展示和动态排序实现
3.1 项目文件和界面UI
项目文件如下:
界面UI:
对于界面中的DataGridView需要注意,启用添加、启用编辑、启用删除、启用列重新排序全部不要勾选。
3.2 代码实现
FrmMain.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListOrder_20191217
{
public partial class FrmMain : Form
{
List<Student> list = null;
public FrmMain()
{
InitializeComponent();
InitList();
}
private void InitList()
{
Student student1 = new Student() { Name = "Lili", Age = 20, Id = "1000", Addr = "广东省广州市" };
Student student2 = new Student() { Name = "Mary", Age = 22, Id = "1004", Addr = "北京市" };
Student student3 = new Student() { Name = "Yang", Age = 25, Id = "1001", Addr = "湖南省长沙市" };
Student student4 = new Student() { Name = "LangLang", Age = 23, Id = "1003", Addr = "湖北省武汉市" };
Student student5 = new Student() { Name = "Chi", Age = 28, Id = "1002", Addr = "广西省桂林市" };
list = new List<Student>() { student1, student2, student3, student4, student5 };
}
private void btnShowStuInfo_Click(object sender, EventArgs e)
{
dgvStuInfo.DataSource = list;
//dgvStuInfo.AutoResizeColumns();
}
private void btnASCOrderById_Click(object sender, EventArgs e)
{
dgvStuInfo.DataSource = list;
list.Sort(new IdASC());
dgvStuInfo.Refresh();
}
private void btnDESCOrderById_Click(object sender, EventArgs e)
{
dgvStuInfo.DataSource = list;
list.Sort(new IdDESC());
dgvStuInfo.Refresh();
}
}
}
Student.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ListOrder_20191217
{
public class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Id { get; set; }
public string Addr { get; set; }
}
/// <summary>
/// 学号升序排列
/// </summary>
public class IdASC : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.Id.CompareTo(y.Id);
}
}
/// <summary>
/// 学号降序排列
/// </summary>
public class IdDESC : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return y.Id.CompareTo(x.Id);
}
}
}