本帖最后由 陈雨 于 2013-4-14 14:31 编辑
这段程序没用调用toString方法,打印的结果不是引用地址值,是不是哪里有复写并调用了toString方法?哪位大神解释下.是不是因为 public static void getStudentInfo(HashMap<String,String> roomMap) 中泛型用了String?
import java.util.*;
class Map07
{
public static void main(String[] args)
{
HashMap<String,HashMap<String,String>> czbk= new HashMap<String,HashMap<String,String>>();
HashMap<String,String> yure = new HashMap<String,String>();
HashMap<String,String> jiuye = new HashMap<String,String>();
czbk.put("yureban",yure);
czbk.put("jiuyeban",jiuye);
yure.put("01","zhangsan");
yure.put("02","wangwu");
jiuye.put("01","zhaoliu");
jiuye.put("02","wangwu");
//遍历czbk集合。获取所有的教室。
Iterator<String> it = czbk.keySet().iterator();
while(it.hasNext())
{
String roomName= it.next();
HashMap<String,String> room =czbk.get(roomName);//room为HashMap中的值
System.out.println(roomName);
//把czbk内的键取出,再取出对应的值,这个值是内部HashMap集合"yure"和"jiuye"
getStudentInfo(room);
}
}
public static void getStudentInfo(HashMap<String,String> roomMap)
{
Iterator<String> it =roomMap.keySet().iterator();//将roomMap对象中的所有的键取出
while(it.hasNext())
{
String id = it.next();//取出的键赋值给id
String name = roomMap.get(id);//根据id的键,取出对应的值
System.out.println(id+":"+name);
}
}
}
|