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());
}
}
}
}