用 HashMap 在内存中保存如下信息:
学号 姓名 成绩
71108501 张三 80.0
71108502 李四 79.5
71108503 王五 91.0
71108504 赵六 60.071108505 宋七 18.6
将姓名成绩储存在一个私有类Data中,将学号作为key,将Data作为value
package test;
import java.util.Map;
import java.util.HashMap;
import java.lang.String;
import java.util.Iterator;
public class HashMapTest {
private class RowData{
private double grades;
private String name;
public RowData(String n, double g){
name = n;
grades = g;
}
public String getName(){
return name;
}
public double getGrades(){
return grades;
}
}
public static void main(String[] args) {
HashMap<String, RowData> info = new HashMap<String, RowData>();
info.put("71108501" , new HashMapTest().new RowData("张三" , 80.0));
info.put("71108502" , new HashMapTest().new RowData("李四" , 79.5));
info.put("71108503" , new HashMapTest().new RowData("王五" , 91.0));
Iterator it = info.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, RowData> entry = (Map.Entry<String, RowData>) it.next();
System.out.println(entry.getKey() + " " +
entry.getValue().getName() + " " + entry.getValue().getGrades());
}
}
}