利用集合实现一个学生信息的增删改查。需要完成以下功能:1)利用集合增加学生对象 2)通过学生的编号(唯一性)修改学生信息 3)通过学生的编号(唯一性)删除学生信息 4)通过输入关键字查询学生信息。 5)采用文件存储方式,实现学生信息的存储和访问
时间: 2024-03-07 07:49:37 浏览: 21
好的,我可以帮你实现这个功能。首先,我们可以定义一个学生类,包括学生的编号、姓名、性别、年龄等属性。然后,我们可以利用HashSet集合来存储学生对象。
以下是示例代码:
```java
import java.util.*;
public class Student {
private int id;
private String name;
private String gender;
private int age;
public Student(int id, String name, String gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
// getter 和 setter 方法
public static void main(String[] args) {
Set<Student> students = new HashSet<>();
// 添加学生对象
students.add(new Student(1, "张三", "男", 18));
students.add(new Student(2, "李四", "女", 19));
students.add(new Student(3, "王五", "男", 20));
// 通过编号修改学生信息
for (Student student : students) {
if (student.getId() == 1) {
student.setName("赵六");
student.setGender("女");
student.setAge(21);
break;
}
}
// 通过编号删除学生信息
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()) {
Student student = iterator.next();
if (student.getId() == 2) {
iterator.remove();
break;
}
}
// 通过关键字查询学生信息
String keyword = "张";
for (Student student : students) {
if (student.getName().contains(keyword)) {
System.out.println(student);
}
}
// 将学生信息存储到文件中
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("students.txt"))) {
oos.writeObject(students);
} catch (IOException e) {
e.printStackTrace();
}
// 从文件中读取学生信息
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("students.txt"))) {
Set<Student> studentsFromFile = (Set<Student>) ois.readObject();
System.out.println(studentsFromFile);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
'}';
}
}
```
以上代码中,我们首先定义了一个Student类,包括学生的编号、姓名、性别、年龄等属性,以及getter和setter方法。然后,我们利用HashSet集合来存储学生对象,并实现了增删改查等操作。最后,我们将学生信息存储到文件中,并从文件中读取学生信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![js](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)