|--需求说明:
编写Java程序,创建3个ArryList对象,每个对象中添加一些学生的姓名。再创建HashMap对象,以年级名称为对象,存放学员的ArrayList值。然后从HashMap对象中获取某个班级的全部学员信息并输出。
|--实现思路:
如需求所说方式
|--代码内容
1 public class Main { 2 static Scanner input = new Scanner(System.in); 3 public static void main(String[] args) { 4 Student stu1 = new Student("张三丰", "男", 7); 5 Student stu2 = new Student("杨过", "男", 8); 6 Student stu3 = new Student("郭靖", "女", 9); 7 Student stu4 = new Student("欧阳锋", "男", 10); 8 Student stu5 = new Student("梅超风", "女", 11); 9 Student stu6 = new Student("韦小宝", "男", 12); 10 11 ArrayList<Student> sanNianSanBan = new ArrayList<>(); 12 sanNianSanBan.add(stu1); 13 sanNianSanBan.add(stu2); 14 sanNianSanBan.add(stu3); 15 16 ArrayList<Student> sanNianErBan = new ArrayList<>(); 17 sanNianErBan.add(stu4); 18 sanNianErBan.add(stu5); 19 sanNianErBan.add(stu6); 20 21 HashMap<String,ArrayList> map = new HashMap<>(); 22 map.put("三年二班",sanNianErBan); 23 map.put("三年三班",sanNianSanBan); 24 25 //抽取 三年二班 的学员信息 26 System.out.println("请输入班级名称:"); 27 String key = input.next(); 28 if (map.containsKey(key)){ 29 ArrayList<Student> list = map.get(key); 30 System.out.println(key+"学生列表"); 31 for (Student student : list) { 32 System.out.println(student.getName()); 33 } 34 }else { 35 System.out.println("没有找到这个班级"); 36 } 37 } 38 }
|--运行结果