import java.util.ArrayList
public class Student {
private String name
private int age
private String gender
public Student(String name, int age, String gender) {
this.name = name
this.age = age
this.gender = gender
}
public String getName() {
return name
}
public int getAge() {
return age
}
public String getGender() {
return gender
}
}
public class Classroom {
private ArrayList<Student>students
public Classroom() {
this.students = new ArrayList<Student>()
}
public void addStudent(Student student) {
students.add(student)
}
public ArrayList<Student>getStudents() {
return students
}
}
public class Main {
public static void main(String[] args) {
Classroom classroom = new Classroom()
// 添加 30 名学生的基本信息
classroom.addStudent(new Student("Tom", 18, "Male"))
classroom.addStudent(new Student("Alice", 19, "Female"))
// ...
// 此处省略 28 名学生的信息
// 获取所有学生的信息
ArrayList<Student>students = classroom.getStudents()
for (Student student : students) {
System.out.println("Name: " + student.getName())
System.out.println("Age: " + student.getAge())
System.out.println("Gender: " + student.getGender())
}
}
}
该代码定义了两个类:Student 类表示一个学生,包含了学生的姓名、年龄和性别等信息;Classroom 类表示一个班级,包含了一个学生的列表,并提供了添加学生和获取学生列表的方法。
在 Main 类的 main 方法中,我们首先实例化一个 Classroom 对象,然后依次添加 30 名学生的信息。最后,我们调用 getStudents 方法获取所
保存,使用java.io.ObjectOutputStream + java.io.FileOutputStream,,,,,,记得要先实现java.io.Serializable读则是使用java.io.ObjectInputstream和java.io.FileInputStream
Student.java
class Student{private String name
private String sex
private int age
public Student(String name, String sex, int age) {
this.name = name
this.sex = sex
this.age = age
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
public String getSex() {
return sex
}
public void setSex(String sex) {
this.sex = sex
}
public int getAge() {
return age
}
public void setAge(int age) {
this.age = age
}
}
Demo.java
public class Demo {public static void main(String[] args) {
ArrayList<Student> list1 = new ArrayList<Student>()
ArrayList<Student> list2 = new ArrayList<Student>()
ArrayList<Student> list3 = new ArrayList<Student>()
Student s1 = new Student("张三丰", "男", 7)
Student s2 = new Student("杨过", "男", 9)
Student s3 = new Student("郭靖", "男", 8)
Student s4 = new Student("张无忌", "男", 7)
Student s5 = new Student("韦一笑", "男", 9)
Student s6 = new Student("宋青书", "男", 8)
Student s7 = new Student("令狐冲", "男", 7)
Student s8 = new Student("任我行", "男", 9)
Student s9 = new Student("林平之", "男", 8)
list1.add(s1)
list1.add(s2)
list1.add(s3)
list2.add(s4)
list2.add(s5)
list2.add(s6)
list3.add(s7)
list3.add(s8)
list3.add(s9)
Map<String, ArrayList<Student>> map = new HashMap<String, ArrayList<Student>>()
map.put("三年级一班", list1)
map.put("三年级二班", list2)
map.put("三年级三班", list3)
Scanner sc = new Scanner(System.in)
System.out.println("输入班级名称:")
String str = sc.next()
ArrayList<Student> arrList = map.get(str)
if (arrList == null) {
System.out.println("没有" + str)
} else {
System.out.println(str + "学生列表:")
for (Student student : arrList) {
System.out.println(student.getName() + " " + student.getSex() + " " + student.getAge())
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)