[ 교실의 학생 정보를 저장하는 HashMap 만들기 ]
public static void main(String[] args) {
// 교실의 학생 정보를 저장하는 HashMap 만들기
// Key : 학번(예: 10101)
// Value : Student 객체
// HashMap 선언 & 생성
Map<Integer, Student> clazz = new HashMap<Integer, Student>();
// Entry 저장
clazz.put(10101, new Student("가길동", new Exam(100, 100, 100)));
clazz.put(10102, new Student("나길동", new Exam(90, 90, 90)));
Integer[] stuNo = {10101, 10102};
for(int i = 0; i < stuNo.length; i++) {
Student student = clazz.get(stuNo[i]);
System.out.println("이름: " + student.getName());
System.out.println("시험: " + student.getName());
}
}
[ 시험 클래스 ]
public class Exam {
private int kro;
private int eng;
private int math;
// new Exam(100, 100, 100)
public Exam(int kro, int eng, int math) {
super();
this.kro = kro;
this.eng = eng;
this.math = math;
}
public int getKro() {
return kro;
}
public void setKro(int kro) {
this.kro = kro;
}
public int getEng() {
return eng;
}
public void setEng(int eng) {
this.eng = eng;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
@Override
public String toString() {
return "Exam [kro=" + kro + ", eng=" + eng + ", math=" + math + "]";
}
[ 학생 클래스 ]
public class Student {
private String name;
private Exam exam;
public Student() {
}
public Student(String name, Exam exam) {
super();
this.name = name;
this.exam = exam;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Exam getExam() {
return exam;
}
public void setExam(Exam exam) {
this.exam = exam;
}