1 package collection; 2 3 public class Student { 4 private String name; 5 private int age; 6 private String sex; 7 8 public Student(String name, int age, String sex) { 9 this.name = name; 10 this.age = age; 11 this.sex = sex; 12 } 13 14 public Student() { 15 } 16 17 public int getAge() { 18 return age; 19 } 20 21 public void setAge(int age) { 22 this.age = age; 23 } 24 25 public String getSex() { 26 return sex; 27 } 28 29 public void setSex(String sex) { 30 this.sex = sex; 31 } 32 33 public String getName() { 34 return name; 35 } 36 37 public void setName(String name) { 38 this.name = name; 39 } 40 41 42 }
1 package collection; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Map; 6 import java.util.Scanner; 7 8 public class Test8 { 9 public static void main(String[] args) { 10 Scanner input=new Scanner(System.in); 11 Student student1=new Student("张三1",18,"男"); 12 Student student2=new Student("张三2",18,"男"); 13 Student student3=new Student("李四1",18,"男"); 14 Student student4=new Student("李四2",18,"男"); 15 Student student5=new Student("张晴1",16,"女"); 16 Student student6=new Student("张晴2",16,"女"); 17 18 ArrayList<Student> list1=new ArrayList(); 19 ArrayList<Student> list2=new ArrayList(); 20 ArrayList<Student> list3=new ArrayList(); 21 22 list1.add(student1); //在list1集合第0个位置添加元素student1 23 list1.add(student2); //在list1集合第1个位置添加元素student2 24 list2.add(student3); //在list2集合第0个位置添加元素student3 25 list2.add(student4); //在list2集合第1个位置添加元素student4 26 list3.add(student5); //在list3集合第0个位置添加元素student5 27 list3.add(student6); //在list3集合第1个位置添加元素student6 28 29 Map<String,ArrayList> m=new HashMap(); 30 m.put("高三1班", list1); 31 m.put("高三2班", list2); 32 m.put("高三3班", list3); 33 34 System.out.println("输入班级名称:"); 35 String schoolClass=input.next(); 36 System.out.println(schoolClass+"学生列表:"); 37 ArrayList<Student> students=m.get(schoolClass); 38 for (Student stu : students) { 39 System.out.println(stu.getName()+"\t"+stu.getSex()+"\t"+stu.getAge()); 40 } 41 42 } 43 }
原文地址:https://www.cnblogs.com/baichang/p/10101197.html
时间: 2024-10-02 12:27:35