本帖最后由 王广亚 于 2013-9-6 17:53 编辑
- import java.util.*;
- class Student1{
- private String id;
- private String name;
- Student1(String id,String name){
- this.name=name;
- this.id=id;
- }
- public String toString(){//第一个问题:下面的代码哪个地方的底层掉用到了toString方法。
- return id+"...."+name;
- }
- }
- public class MapDemo3 {
- public static void demo() {
- HashMap<String,List<Student1>> czbk=new HashMap<String,List<Student1>>();
- List<Student1> reyu=new ArrayList<Student1>();
- List<Student1> jiuye=new ArrayList<Student1>();
- czbk.put("yureban", reyu);
- czbk.put("jiuyeban", jiuye);
- reyu.add(new Student1("01","zhangsan"));
- reyu.add(new Student1("02","lisi"));
- jiuye.add(new Student1("01","zhaoliu"));
- jiuye.add(new Student1("02","wangwu"));
- Iterator<String> it=czbk.keySet().iterator();
- while(it.hasNext()){
- String roomName=it.next();
- List<Student1> room=czbk.get(roomName);
- getInfos(room);
- }
-
- }
- private static void getInfos(List<Student1> room) {
- Iterator<Student1> it=room.iterator();
- while(it.hasNext()){
- Student1 s=it.next();
- System.out.println(s);//第二个问题:此处打印的是Student1的对象,怎么回事?难道这两个问题有关联?
- }
-
- }
- public static void main(String args[]){
- demo();
- }
- }
复制代码 |