定义一个学生类Student,包含学号、姓名、性别、专业等属性,然后使用ArrayList类实现一个学生线性表,按学号进行排序,并尝试插入删除操作。
4条回答 默认 最新
- HerryDong 2022-10-20 15:44关注
using System; using System.Collections; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Student student1 = new Student { Id = 3, Name = "张三", Gender = "男", Major = "计算机专业" }; Student student2 = new Student { Id = 2, Name = "李四", Gender = "女", Major = "计算机专业" }; Student student3 = new Student { Id = 1, Name = "王五", Gender = "男", Major = "计算机专业" }; Student student4 = new Student { Id = 4, Name = "赵六", Gender = "女", Major = "计算机专业" }; // 插入数据 ArrayList list = new ArrayList(); list.Add(student1); list.Add(student2); list.Add(student3); list.Add(student4); // 根据Id排序 StudentComparer comparer = new StudentComparer(); list.Sort(comparer); foreach (Student item in list) { Console.WriteLine(item.Id + "," + item.Name); } // 删除性别为女的数据 for (int i = list.Count - 1; i >= 0; i--) { Student student = list[i] as Student; if (student.Gender == "女") { list.RemoveAt(i); } } Console.ReadKey(); } } public class StudentComparer : IComparer { public int Compare(object x, object y) { Student a = x as Student; Student b = y as Student; if (a.Id < b.Id) { return -1; } else if (a.Id > b.Id) { return 1; } else { return 0; } } } public class Student { /// <summary> /// 学号 /// </summary> public int Id { get; set; } /// <summary> /// 姓名 /// </summary> public string Name { get; set; } /// <summary> /// 性别 /// </summary> public string Gender { get; set; } /// <summary> /// 专业 /// </summary> public string Major { get; set; } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用
悬赏问题
- ¥15 matlab中使用gurobi时报错
- ¥15 WPF 大屏看板表格背景图片设置
- ¥15 这个主板怎么能扩出一两个sata口
- ¥15 不是,这到底错哪儿了😭
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂