1.0 为什么使用集合
2.0 泛型集合List
创建一个控制台程序。
Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoList
{
class Student
{
public Student() { }
public Student(int StudentNo, string StudentName,int Age) {
this.StudentNo = StudentNo;
this.StudentName = StudentName;
this.Age = Age;
}
public int StudentNo { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoList
{
class Program
{
public class Teacher
{
private string name;
private int age;
public string Name { get; set; }
public int Age { get; set; }
}
static void Main(string[] args)
{
//实例化List<T>集合对象
List<Student> list = new List<Student>();
//添加对象元素
Student objStu1 = new Student() { Age = 20, StudentNo = 1001, StudentName = "小张" };
Student objStu2 = new Student() { Age = 22, StudentNo = 1003, StudentName = "小李" };
Student objStu3 = new Student() { Age = 22, StudentNo = 1002, StudentName = "小王" };
list.Add(objStu1);
list.Add(objStu2);
list.Add(objStu3);
//Teacher objTeacher = new Teacher() { Name = "王老师", Age = 28 };
//list.Add(objTeacher);
//遍历元素,取出元素时不需要强制转换
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StudentName, list[i].Age, list[i].StudentNo);
}
Console.WriteLine("******************************************************************");
//集合初始化器:使用基本数据类型元素
List<string> nameList = new List<string>()
{
"小张","小李","小秦"
};
//集合初始化器和对象初始化器共同使用
List<Student> stuList = new List<Student>()
{
new Student() { Age = 20, StudentNo = 1001, StudentName = "小张" },
new Student() { Age = 22, StudentNo = 1003, StudentName = "小李" },
new Student() { Age = 22, StudentNo = 1002, StudentName = "小王" }
};
foreach (string item in nameList)
{
Console.WriteLine(item);
}
foreach (Student stu in stuList)
{
Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", stu.StudentName, stu.Age, stu.StudentNo);
}
Console.ReadLine();
}
}
}
3.0 泛型集合Dictionary<K,V>
//实例化集合对象
Dictionary<string, Student> stus = new Dictionary<string, Student>();
//添加对象元素
Student stu1 = new Student() { Age = 20, StudentNo = 1002, StudentName = "小张" };
Student stu2 = new Student() { Age = 22, StudentNo = 1003, StudentName = "小李" };
Student stu3 = new Student() { Age = 22, StudentNo = 1002, StudentName = "小王" };
stus.Add("小张", objStu1);
stus.Add("小李", objStu2);
Console.WriteLine("小李的年龄:{0}", stus["小李"].Age);//通过key查找value
stus.Add("小王", objStu3);
Console.WriteLine("小李的年龄:{0}", stus["小李"].Age);
//遍历key
foreach (string item in stus.Keys)
{
Console.WriteLine(item);
}
//遍历values
foreach (Student stu in stus.Values)
{
Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", stu.StudentName, stu.Age, stu.StudentNo);
}
4.0 集合对象的排序
//集合初始化器:使用基本数据类型元素
List<string> names = new List<string>()
{
"小张","小李","小秦","小东","小唐","小白"
};
Console.WriteLine("-------------字符串默认排序--------------");
foreach (string item in names)
{
Console.WriteLine(item);
}
names.Sort();//使用Sort()方法对集合中元素排序(默认升序)
Console.WriteLine("-------------字符串排序后--------------");
foreach (string item in names)
{
Console.WriteLine(item);
}
names.Reverse();//集合元素反转
Console.WriteLine("-------------元素反转后--------------");
foreach (string item in names)
{
Console.WriteLine(item);
}
如果是值类型排序,效果如下:
//集合初始化器:使用基本数据类型元素
List<int> ageList = new List<int>()
{
20,27,21,26,28,30
};
Console.WriteLine("-------------整数元素默认排序--------------");
foreach (int item in ageList)
{
Console.WriteLine(item);
}
ageList.Sort();//使用Sort()方法对集合中元素排序(默认升序)
Console.WriteLine("-------------整数元素排序后--------------");
foreach (int item in ageList)
{
Console.WriteLine(item);
}
ageList.Reverse();//集合元素反转
Console.WriteLine("-------------元素反转后--------------");
foreach (int item in ageList)
{
Console.WriteLine(item);
}
对象的排序,不能直接排序对象本身。如下代码会报错:
这时候,我们可以修改一下Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoList
{
class Student:IComparable<Student>
{
public Student() { }
public Student(int StudentNo, string StudentName,int Age) {
this.StudentNo = StudentNo;
this.StudentName = StudentName;
this.Age = Age;
}
public int StudentNo { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
/// <summary>
/// 接口的实现方法
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(Student other)
{
//升序
return other.StudentNo.CompareTo(this.StudentNo);
}
}
}
这时在主方法中实现代码,可看到排序效果:
//实例化List<T>集合对象
List<Student> lists = new List<Student>();
//添加对象元素
Student stu01 = new Student() { Age = 20, StudentNo = 1001, StudentName = "小张" };
Student stu02 = new Student() { Age = 22, StudentNo = 1003, StudentName = "小李" };
Student stu03 = new Student() { Age = 22, StudentNo = 1002, StudentName = "小王" };
lists.Add(stu01);
lists.Add(stu02);
lists.Add(stu03);
lists.Sort();//将对象元素排序
for (int i = 0; i < lists.Count; i++)
{
Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", lists[i].StudentName, lists[i].Age, lists[i].StudentNo);
}
Console.ReadLine();
到这里,排序方案是在实体类方法中写死的。那么如何灵活处理排序方案呢?
在Student.cs中新增4个排序类:
namespace DemoList
{
class Student:IComparable<Student>
{
……
}
//年龄升序排列
class AgeASC : IComparer<Student>
{
public int Compare(Student x, Student y)
{
//return x.Age - y.Age;
return x.Age.CompareTo(x.Age);
}
}
//年龄降序排列
class AgeDESC : IComparer<Student>
{
public int Compare(Student x, Student y)
{
//return y.Age - x.Age;
return y.Age.CompareTo(x.Age);
}
}
//姓名升序排列
class NameASC : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.StudentName.CompareTo(y.StudentName);
}
}
//姓名降序排列
class NameDESC : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return y.StudentName.CompareTo(x.StudentName);
}
}
}
主函数测试代码:
//实例化List<T>集合对象
List<Student> list = new List<Student>();
//添加对象元素
Student stu01 = new Student() { Age = 20, StudentNo = 1001, StudentName = "小张" };
Student stu02 = new Student() { Age = 22, StudentNo = 1003, StudentName = "小李" };
Student stu03 = new Student() { Age = 22, StudentNo = 1002, StudentName = "小王" };
list.Add(stu01);
list.Add(stu02);
list.Add(stu03);
//默认排序
Console.WriteLine("------------默认排序------------");
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StudentName, list[i].Age, list[i].StudentNo);
}
//年龄降序排序
Console.WriteLine("------------年龄降序排序------------");
list.Sort(new AgeDESC());
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StudentName, list[i].Age, list[i].StudentNo);
}
//姓名升排序
Console.WriteLine("------------姓名升序排序------------");
list.Sort(new NameASC());
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine("姓名:{0} 年龄:{1} 学号:{2}", list[i].StudentName, list[i].Age, list[i].StudentNo);
}
运行:
选中Sort方法按F1键:
List集合Sort方法的重点总结如下:
END