我是靠谱客的博主 纯真乌冬面,这篇文章主要介绍在集合中增删改查学生信息,现在分享给大家,希望可以做个参考。

 题目要求;

1.新建学生类Student
        学号    int
        姓名    String
        生日    java.util.Date
        
    重写equals方法     学号相等即认为两个学生相同
    重写toString方法     返回学号,姓名和生日(生日的格式是yyyy-MM-dd)
    
2.在Test2类中的main方法中声明一个长度是50的学生数组  -> 学生集合
     在控制台上完成新增学生,查看学生功能
     用户选择新增功能后:
         提示用户
                 输入学号
                 输入姓名
                 输入生日(yyyy-MM-dd)     将字符串转成日期存到Student的对象的生日属性上
                 
                 将学生对象放入数组
   用户选择查看学生功能后
           提示用户
                   输入学号
                     
                     根据输入的学号在数组中查找学生是否存在,如果有,调用toString显示
                     如果没有提示“查无此人”
                 
                 
 用户选择删除功能
         提示用户
                 输入学号
                 
                 在数组中查找学号是否存在,如果存在将学生对象从数组中移除,   在该学生对象所处数组位置之后的所有学生对象应向前移动一位
                 count--;
             
                 如果没有提示“查无此人”
                 
用户选择修改功能
        提示用户                      
                 输入学号    (学号是不允许修改的)
                 在数组中查找学号是否存在,如果存在,
                 提示请输入新的姓名     (提示之前也可以将旧的姓名显示出)    
                 提示请输入新的生日     (提示之前也可以将旧的生日显示出)
                 
                 如果没有提示“查无此人”
                 
 
--6.显示所有学生信息
                遍历数组将所有信息打印
                
                *按学号升序排序后打印
 

新建的学生类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Student { private int sno; //学号 private String sname; //姓名 private Date birthday; //生日 public Student() {} public Student(int sno, String sname, Date birthday) { this.sno = sno; this.sname = sname; this.birthday = birthday; } @Override public boolean equals(Object obj) { if(this == obj) { return true; } if(!(obj instanceof Student)) { return false; } Student temp = (Student)obj; return this.sno == temp.getSno(); } @Override public String toString() { String info = this.sno+"t"+this.sname+"t"+DateUtil.date2String(birthday); return info; } public int getSno() { return sno; } public void setSno(int sno) { this.sno = sno; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }

新建一个工具类,用来转换输入的日期的格式

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class DateUtil { /** * 将传入的date对象,按照yyyy-MM-dd的格式进行解析,返回解析后的字符串 * @param date * @return */ public static String date2String(Date date) { return date2String(date, null); } /** * 将传入的date对象,按照fmt的格式进行解析,返回解析后的字符串 * @param date * @param fmt * @return */ public static String date2String(Date date, String fmt) { if(date == null) { return null; } //判断字符串是否有内容 if(fmt == null || fmt.isEmpty()) { //如果外部没有传入格式,默认使用yyyy-MM-dd fmt = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(fmt); return sdf.format(date); } /** * 将传入的str对象,按照yyyy-MM-dd的格式进行解析,返回解析后得到的日期类型对象 * @param str * @return */ public static Date string2Date(String str) { return string2Date(str, null); } /** * 将传入的str对象,按照fmt的格式进行解析,返回解析后得到的日期类型对象 * @param str * @param fmt * @return */ public static Date string2Date(String str, String fmt) { if(str == null) { return null; } //判断字符串是否有内容 if(fmt == null || fmt.isEmpty()) { //如果外部没有传入格式,默认使用yyyy-MM-dd fmt = "yyyy-MM-dd"; } SimpleDateFormat sdf = new SimpleDateFormat(fmt); Date d = null; try { d = sdf.parse(str); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return d; } }

测试类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class Test2 { public static void main(String[] args) { List<Student> list = new ArrayList<Student>(); while(true) { System.out.println("1-新增学生:"); System.out.println("2-修改学生:"); System.out.println("3-删除学生:"); System.out.println("4-查看学生:"); System.out.println("5-退出:"); System.out.println("请输入您的选择:"); Scanner sc = new Scanner(System.in); int sel = sc.nextInt(); if(sel == 1) { //新增 //接收控制台输入的内容 System.out.println("请输入学号:"); int sno = sc.nextInt(); System.out.println("请输入姓名:"); String sname = sc.next(); System.out.println("请输入生日(yyyy-MM-dd):"); String b = sc.next(); Date birthday = DateUtil.string2Date(b); //将内容组装成Student对象 Student s = new Student(sno, sname, birthday); //将s放入到数组ss之前应该判断s是否在数组中已存在 //代码略... list.add(s); }else if(sel == 2) { //修改 System.out.println("请输入学号:"); int sno = sc.nextInt(); Student temp = new Student(); temp.setSno(sno); if(list.contains(temp)) { //遍历集合找到temp这个人的下标 int index = 0; for(int i = 0; i < list.size(); i++) { if(temp.equals(list.get(i))) { index = i; } } //输入新的姓名和新的生日 System.out.println("请输入姓名:"); String sname = sc.next(); System.out.println("请输入生日(yyyy-MM-dd):"); String b = sc.next(); Date birthday = DateUtil.string2Date(b); temp.setSname(sname); temp.setBirthday(birthday); //通过set(int index, Student temp); list.set(index, temp); }else { System.out.println("查无此人"); } }else if(sel == 3) { //删除 System.out.println("请输入学号:"); int sno = sc.nextInt(); Student temp = new Student(); temp.setSno(sno); if(list.contains(temp)) { list.remove(temp); System.out.println("删除成功"); }else { System.out.println("查无此人"); } }else if(sel == 4) { //查看 //接收控制台输入的内容 System.out.println("请输入学号:"); int sno = sc.nextInt(); //在ss中遍历sno是否存在 boolean f = false; //开关:不存在 for(Student temp : list) { if(temp != null && temp.getSno() == sno) { System.out.println(temp); f = true; //开关:存在 break; } } if(!f) { System.out.println("查无此人"); } }else { break; } System.out.println("====================="); } } }

 

转载于:https://www.cnblogs.com/zhangMY12138/p/9418360.html

最后

以上就是纯真乌冬面最近收集整理的关于在集合中增删改查学生信息的全部内容,更多相关在集合中增删改查学生信息内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部