创建一个HashMap泛型对象,添加学员姓名分数,键为学员姓名值为学员对象,从HashMap中用迭代器打印学员成绩 5
2个回答
展开全部
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);
}
}
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);
}
}
展开全部
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;
}
}
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;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询