作业(补)8

作业:
作业(补)8_第1张图片

本题目使用Map集合;

编写Student类:

复制代码
package com.jihe.zuoye;

public class Student {
    private String name;
    private String sex;

    public Student() {
    }

    public Student(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}
复制代码

编写StudentMap类:

复制代码
package com.jihe.zuoye;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class StudentMap {
    public static void main(String[] args) {
        Student stu1 = new Student("张三","男");
        Student stu2 = new Student("王四","女");
        Student stu3 = new Student("赵五","男");

        Map map = new HashMap();
        map.put("Jo",stu1);
        map.put("Lily",stu2);
        map.put("Smith",stu3);

        Set keys = map.keySet();
        for(String key:keys){
            System.out.println(key+"的中文名为:"+map.get(key).getName()+",性别为:"+map.get(key).getSex());

        }

    }
}
复制代码

输出结果为:

Jo的中文名为:张三,性别为:男
Smith的中文名为:赵五,性别为:男
Lily的中文名为:王四,性别为:女

你可能感兴趣的:(作业(补)8)