才学习Java不久。老师让做一个学员管理系统。在添加学员信息类时要求使用HashMap存放多个Student对象。
Student类里面就是几个变量,提供了get,set方法。
请问:我在添加业务这边怎么使用HashMap来存放多个Student类,怎么读取HaspMap里面Student对象里面的属性,具体怎么使用。能否给出实例代码。还有个问题呀,像我这种代码需要使用什么开发模式吗?谢谢啦。才学Java实在很菜。哪位大虾有空看下。不胜感激!
1.// 创建一个HashMap对象
2.Map stuMap = new HashMap();
3.
4.// 把一个学生对象存储进HashMap
5.Student stu1 = new Student("1001", "张三");
6.stuMap.put(stu1.getId(), stu1);
Set set = stuMap.keySet();
Iterator it=set.iterator(); while (it.hasNext()) {
String key=it.next();
Student stu2 = stuMap.get(key);
}
HashMap 就是一个键值对方式存储。
[code="java"]
// 创建一个HashMap对象
Map stuMap = new HashMap();
// 把一个学生对象存储进HashMap
Student stu1 = new Student("1001", "张三");
stuMap.put(stu1.getId(), stu1);
// 取出一个学生对象
Student stu2 = stuMap.get("1001");
[/code]