定义一个学生类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 对于这个问题的代码运行
- ¥50 三种调度算法报错 有实例
- ¥15 关于#python#的问题,请各位专家解答!
- ¥200 询问:python实现大地主题正反算的程序设计,有偿
- ¥15 smptlib使用465端口发送邮件失败
- ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
- ¥15 对于squad数据集的基于bert模型的微调
- ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
- ¥20 steam下载游戏占用内存
- ¥15 CST保存项目时失败