(실습) set를 사용한 성적 처리

DeokHun KIM·2022년 7월 21일
0

java

목록 보기
27/30
//1. 3명의 학생데이터(성명,국어,영어,수학)를 
//   StudentVO 클래스를 이용해서 만들고
//2. HashSet<StudentVO> 타입의 변수(set)에 저장하고 출력
StudentVO stu1 = new StudentVO("홍길동", 100, 90, 81);
StudentVO stu2 = new StudentVO("이순신", 95, 88, 92);
StudentVO stu3 = new StudentVO("김유신", 90, 87, 77);

HashSet<StudentVO> hash = new HashSet<StudentVO>();
hash.add(stu1);
hash.add(stu2);
hash.add(stu3);

Iterator<StudentVO> itStu = hash.iterator();
while (itStu.hasNext()) {
    Syetem.out.println(itStu.next().toString());
}

김유신 90 87 77 254 84.66
홍길동 100 90 81 271 90.33
이순신 95 88 92 275 91.66


//김유신 국어 점수를 95 점으로 수정 후 전체데이터 출력
while (itStu.hasNext()) {
	StudentVO stu = itStu.next();
	if (stu.getName().equals("김유신")); {
    	stu.setKor(95);
    }
    System.out.println(stu.toString());
}

위의 코드대로 짜면 안나온다. 이유는 iterator는 일회용 이라 next() 를 위해 한번더 선언해줘야 한다

//김유신 국어 점수를 95 점으로 수정 후 전체데이터 출력
itStu = hash.iterator();
while (itStu.hasNext()) {
	StudentVO stu = e.next();
	if (stu.getName().equals("김유신")); {
    	stu.setKor(95);
    }
    System.out.println(stu.toString());
}

김유신 95 87 77 259 86.33
홍길동 100 90 81 271 90.33
이순신 95 88 92 275 91.66


//"김유신" 학생 성적만 출력
itStu = hash.iterator();
while (itStu.hasNext()) {
	StudentVO stu = itStu.next();
    if (stu.getName.equals("김유신")) {
    	System.out.println(stu.toString());
    }
}

김유신 95 87 77 259 86.33

0개의 댓글