import java.util.HashSet; import java.util.Set; /** * 学生类 * @author Administrator * */ public class StudentCls { public String name; public String id; // public Set<CourseCls> couSet; public StudentCls(String name,String id){ this.name=name; this.id=id; // this.couSet=new HashSet<CourseCls>(); } } import java.util.*; public class MapTest { public Map<String, StudentCls> students; public void putTest(){ Scanner sca=new Scanner(System.in); int i=0; while(i<3){ System.out.println("请输入学生ID"); String ID=sca.next(); StudentCls stu=students.get(ID); if(stu==null){ System.out.println("请输入学生姓名:"); String name=sca.nextLine(); StudentCls newstu=new StudentCls(name, ID); students.put(ID, newstu); System.out.println("成功添加学生"+students.get(ID).name); i++; } else{ System.out.println("已有该学生!"); } } } public void ketSet(){ Set<String> keyset=students.keySet(); System.out.println("总共有"+keyset.size()+"个学生"); for(String id:keyset){ StudentCls stu=students.get(id); if(stu!=null){ System.out.println("学生:"+stu.name); } } } public static void main(String[] args) { MapTest mt=new MapTest(); mt.putTest(); mt.ketSet(); } }
guozhchun
相关分类