创建一个HashMap泛型对象,添加学员姓名分数,键为学员姓名值为学员对象,从HashMap中用迭代器打印学员成绩 5

 我来答
andygulin
推荐于2016-02-12 · TA获得超过1678个赞
知道小有建树答主
回答量:1362
采纳率:0%
帮助的人:1100万
展开全部
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Test {

public static void main(String args[]) {
Map<String, Student> students = new HashMap<String, Student>();
students.put("aaa", new Student(60));
students.put("bbb", new Student(70));
students.put("ccc", new Student(80));
for (Entry<String, Student> entry : students.entrySet()) {
System.out.println("姓名:" + entry.getKey() + " 成绩:" + entry.getValue());
}
}
}

class Student {

public Student(int point) {
this.point = point;
}

private int point;

public int getPoint() {
return point;
}

public void setPoint(int point) {
this.point = point;
}

@Override
public String toString() {
return String.valueOf(this.point);
}

}
百度网友59d35ce
推荐于2018-03-02 · TA获得超过137个赞
知道小有建树答主
回答量:115
采纳率:100%
帮助的人:85.6万
展开全部
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class TestHashMap {
public static void main(String[] args) {
Map<String, Student> map = new HashMap<String, Student>();
Student stu1 = new Student("张三",89);
Student stu2 = new Student("李四",90);
Student stu3 = new Student("王五",91);//姓名不要一样,不然被覆盖
map.put(stu1.getName(), stu1);
map.put(stu2.getName(), stu2);
map.put(stu3.getName(), stu3);
for(Iterator<Map.Entry<String, Student>> it = map.entrySet().iterator(); it.hasNext();){
Entry<String, Student> entry = it.next();
System.out.println("姓名:"+entry.getKey()+" 成绩:"+entry.getValue().getScore());
}
}
}

class Student{
String name;
double score;
public Student(){

}
public Student(String name,double score){
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}

}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式