元素自动变化的集合List<T>
List<T>泛型集合的特点
- <T>表示泛型,T是TYPEd的简写,表示当前不确定类型。
- 可以根据用户实际需要,确定当前集合需要存放的数据类型,一旦确定不可改变。
- 使用Add方法可以随意添加元素,没有个数限制。
- 使用Remove方法可以删除指定元素,元素删除后索引自动调整。
- 通过索引访问元素。
List<T>泛型集合的存储结构
- 索引 0 - 1-2-3-4…….
List<T>使用前的准备工作
- 引入命名空间。
- 确定存储类型。比如List<Student>Student=new List<Student>();
- -
常用方法
- 添加元素:Add(<T>)
- 删除元素:RemoveAt(索引)
常用属性:元素个数:Count
- 集合的遍历方法
foreach(Student stu in Students)
{
Console.WriteLine(stu.StudentName);
}
List<T>泛型集合应用
典型应用
static void Main(string[] args)
{
Student objStu1 = new Student(1000, "Mike", 30);
Student objStu2 = new Student(1002, "Jerry", 29);
Student objStu3 = new Student(1004, "Shark", 28);
//创建List泛型,并将对象添加到students
List<Student> students = new List<Student>();
students.Add(objStu1);
students.Add(objStu2);
students.Add(objStu3);
//使用对象初始化器添加到泛型集合
students.Add(new Student(1006, "c#", 27));
students.Add(new Student(1008, "python", 28));
//获取元素总数
Console.WriteLine("元素总数{0}", students.Count);
//以对象的方式删除一个元素
students.Remove(objStu3);
//索引的方式删除一个元素
students.RemoveAt(0);
//插入一个对象
students.Insert(0, new Student(1010, "Shark", 26));
//遍历集合
foreach (Student stu in students)
{
Console.WriteLine(stu.StudentId+"\t"+stu.StudentName);
}
严格的类型约束
集合初始化器
在.net3.0以后的版本中,集合初始化器被应用出来。
//基本数据类型元素初始化
List<string>list = new List<string>()
{
"小王","小强","小赵","小李"
};
foreach(string item in list)
{
Console.WriteLine(item);
}
//对象初始化器和集合初始化器共同使用
List<Student> stuList=new List< Student >
{
new Student() { StudentId=1000,StudentName="Tom",Age=28},
new Student() {StudentName="Jerry",Age=26,StudentId=1002 }
};
foreach(Student stu in stuList)
{
Console.WriteLine("学号{0},年龄{1},姓名{2}",stu.StudentId,stu.Age,stu.StudentName);
}
for (int i = 0; i < stuList.Count; i++)
{
Student listStu = stuList[i];
Console.WriteLine(listStu.StudentName);
}
元素自动变化的集合
Student objStu1 = new Student() { StudentId = 1000, StudentName = "小文", Age = 20 };
Student objStu2 = new Student() { StudentId = 1002, StudentName = "小宏", Age = 22 };
Dictionary<string, Student> stus = new Dictionary<string, Student>();
stus.Add("小文", objStu1);
stus.Add("小宏", objStu2);
foreach(string key in stus.Keys)
{
Console.WriteLine(key);
}
foreach(Student value in stus.Values)
{
Console.WriteLine("学号 {0},姓名 {1}",value.StudentId,value.StudentName);
}
基本数据类型排序
//对于字符串元素默认按照拼音首字母升序排列
//基本数据类型可以直接排序
List<string> list = new List<string>()
{
"小王","小强","小刘","小李","候平"
};
foreach(string item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("******排序后***********");
list.Sort();//调用Sort()方法直接排序元素
foreach(string item in list)
{
Console.WriteLine(item);
}
Console.WriteLine("******元素反转***********");
list.Reverse();//调用Reverse()方法,实现元素反转
foreach(string item in list)
{
Console.WriteLine(item);
}
对象类型元素的排序
对象不能直接排序
对象有若干个属性。
正因为对象有多个属性,这样排序的时候,系统默认是不知道应该按照哪个属性排序的。解决办法就是得明确告诉排序的属性是什么。
对象的默认排序方法: 将需要排序的对象实现泛型接口IComparable<T>按学号排序
在类文件输入:IComparable<T>选择实现接口:
//设置接口方法
比较有三中结果:<0(后者小于前者),0(相等),>0(大于)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 集合在OOP中的应用
{/// <summary>
/// 实现了排序接口的类
/// </summary>
class Student: IComparable<Student>
{
public Student() { }
public Student(int StudentId, string StudentName, int Age)
{
this.StudentId = StudentId;
this.StudentName = StudentName;
this.Age = Age;
}
private int studentId;
private string studentName = string.Empty;
private int age;
public int StudentId { get { return studentId; } set { studentId = value; } }
public string StudentName { get { return studentName; } set { studentName = value; } }
public int Age { get { return age; } set { age = value; } }
/// <summary>
/// 泛型接口实现的排序方法
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(Student other)
{
//接口方法
return this.StudentId.CompareTo(other.StudentId);
//实现升序
//return other.StudentId.CompareTo(this.StudentId);
}
}
}
根据姓名排序
! 默认排序只能有一种
集合中对象的动态排序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 集合在OOP中的应用
{
class Student: IComparable<Student>
{
public int CompareTo(Student other)
{
return this.StudentName.CompareTo(other.StudentName);
//return other.StudentId.CompareTo(this.StudentId);
}
public Student() { }
public Student(int StudentId, string StudentName, int Age)
{
this.StudentId = StudentId;
this.StudentName = StudentName;
this.Age = Age;
}
private int studentId;
private string studentName = string.Empty;
private int age;
public int StudentId { get { return studentId; } set { studentId = value; } }
public string StudentName { get { return studentName; } set { studentName = value; } }
public int Age { get { return age; } set { age = value; } }
}
//添加4个排序类,并且分别实现排序接口
class StudentNameASC : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return x.StudentName.CompareTo(y.StudentName);
}
}
class StudentNameDEsc : IComparer<Student>
{
public int Compare(Student x, Student y)
{
return y.StudentName.CompareTo(x.StudentName);
}
}
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);
}
}
}
//对象动态排序
Student objStu1 = new Student() { StudentId = 1002, StudentName = "Tom", Age = 28 };
Student objStu2 = new Student() { StudentId = 1004, StudentName = "Jerry", Age = 26 };
Student objStu3 = new Student() { StudentId = 1006, StudentName = "Meki", Age = 24 };
Student objStu4 = new Student() { StudentId = 1008, StudentName = "php", Age = 22 };
List<Student> stuList = new List<Student>() { objStu4, objStu2, objStu1, objStu3 };
foreach (Student item in stuList)
{
Console.WriteLine(item.StudentName);
}
Console.WriteLine("**************姓名升序**********");
stuList.Sort(new StudentNameASC());
foreach (Student item in stuList)
{
Console.WriteLine(item.StudentName);
}
Console.WriteLine("**************姓名降序***********");
stuList.Sort(new StudentNameDEsc());
foreach(Student item in stuList)
{
Console.WriteLine(item.StudentName);
}
Console.WriteLine("*************年龄升序************");
stuList.Sort(new AgeASC());
foreach (Student item in stuList)
{
Console.WriteLine(item.Age);
}
Console.WriteLine("***************年龄降序**********");
stuList.Sort(new AgeDESC());
foreach (Student item in stuList)
{
Console.WriteLine(item.Age);
}