集合优点:集合不用规定空间大小,而数组则用。
集合与数组相互转换:
Course course4 = new Course(3,"huang",23); Course course5 = new Course(4,"huang",24); Course course6 = new Course(5,"huang",25); //集合不用规定空间大小,而数组则用。 List<Course> courses = new List<Course>() { course4,course5,course6,course4}; Course[] courses1= new Course[] { course6, course5, course4, course4 }; courses.AddRange(courses1);//把数组添加到集合中来 Course[] courses2 = courses.ToArray();//集合转数组 List<Course> courses3 = courses1.ToList();//数组转集合
删除集合元素:
courses.Remove(course4);//删除第一个course4 courses.RemoveAll(c => c.Id == 4);//删除id等于4的,==,>,< courses.RemoveAt(0);//删除下标 courses.RemoveRange(1, 2);//从第一个过后,删除两个--2和3
循环遍历:
for (int i = 0; i < courses.Count; i++) { Console.WriteLine($"我是{courses[i].Name},第{courses[i].Id}号,今年{courses[i].age}岁"); } foreach (Course course in courses) { Console.WriteLine($"{course.Name}\t{course.Id}\t{course.age}"); }
查询元素:
List<Course>c1 =courses.FindAll(c => c.Id > 3); var c2=from c in courses where c.Id == 3 select c;
排序:
标签:Course,courses,course4,集合,new,Id From: https://www.cnblogs.com/lin-07/p/17378203.html