- package test5;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Set;
- public class Test1 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- /*
- * for(int i=0;i<5;i++){ for(int j=0;j<=i;j++){ System.out.print("*"); }
- * System.out.println(); }
- */
- Map<String, Classes> m1 = new HashMap<String, Classes>();
- Student s1 = new Student(1, "张三", 18);
- Student s2 = new Student(2, "李四", 20);
- Student s3 = new Student(3, "王五", 21);
- Student s4 = new Student(4, "李磊", 19);
- Student s5 = new Student(5, "韩梅梅", 10);
- Student s6 = new Student(6, "露丝", 11);
- Student s7 = new Student(7, "才衣领", 84);
- Student s8 = new Student(8, "荣组二", 73);
- Student s9 = new Student(9, "周结论", 97);
- Set<Student> ss1 = new HashSet<Student>();
- Set<Student> ss2 = new HashSet<Student>();
- Set<Student> ss3 = new HashSet<Student>();
- ss1.add(s1);
- ss1.add(s2);
- ss1.add(s3);
- ss2.add(s4);
- ss2.add(s5);
- ss2.add(s6);
- ss3.add(s7);
- ss3.add(s8);
- ss3.add(s9);
- Classes c1 = new Classes(1, "物理", ss1);
- Classes c2 = new Classes(2, "英文", ss2);
- Classes c3 = new Classes(3, "革命", ss3);
- m1.put("c1", c1);
- m1.put("c2", c2);
- m1.put("c3", c3);
- Set<Student> set1 = new HashSet<Student>();
- // 遍历Map
- for (Map.Entry<String,Classes> entry : m1.entrySet()) {
- set1 = entry.getValue().getStudents();
- for (Student s : set1) {
- System.out.println("学生姓名:" + set1.iterator().next().getName());
- }
- }
- }
- }
复制代码 这个很简单,就是把学生放到班级里面,然后把班级放到一个MAP里面,然后遍历这个MAP得到所有的班级,然后再遍历一遍得到所有的学生,班级Classes和学生Student这两个类比较简单就不写了,但是上面的代码除了错误。得到的结果如下:
学生姓名:李磊
学生姓名:李磊
学生姓名:李磊
学生姓名:荣组二
学生姓名:荣组二
学生姓名:荣组二
学生姓名:李四
学生姓名:李四
学生姓名:李四
这个程序在哪里出错了呢?? 请大家帮忙看一下 |