Q5.
List <Board> list = new ArrayList<Board>();
Q6.
Map<String, Integer> 변수명 = new HashMap<String,Integer>();
Q7.
더보기
List getBoarList(){
//ArrayList 객체생성
List<Board> list = new ArrayList<Board>();
//자료 넣기
list.add(new Board("재목1", "내용1"));
list.add(new Board("재목2", "내용2"));
list.add(new Board("재목3", "내용3"));
return list;
}
Q8
더보기
@Override
public int hashCode() {
return studentNum;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Student) {
Student std = (Student) obj;
if(std.studentNum == studentNum) {
return true;
}
}
return false;
}
Q9
더보기
package check;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("blue", 96);
map.put("hong", 86);
map.put("white", 92);
String maxName = null;
String minName = null;
String name = null;
int maxScore =0;
int minScore = 100;
int totalScore = 0;
//작성위치
//평균점수 = 모든 사람의 점수 합 getValue()
//1단계 Map.Entry 구하기
Set<Entry<String,Integer>> entrySet = map.entrySet();
//2단계 getValue() 이용해서 합계 구하기
for(Map.Entry<String, Integer> entry : entrySet) {
//최고 점수의 아이디 : 모든 점수를 비교해서 가장 큰 점수의 키값 getKey()
if(entry.getValue() > maxScore) {maxScore = entry.getValue(); maxName = entry.getKey();}
//최저점수의 아이디 :모든 점수를 비교해서 가장 적은 점수의 키값
if(entry.getValue() < minScore) {minScore = entry.getValue(); minName = entry.getKey();}
//합계 누적
totalScore += entry.getValue();
}
//평균 구하기
double avg = totalScore / (double) map.size();
System.out.println("평균점수 : " + avg);
System.out.println("최고점수 : " + maxScore);
System.out.println("최고점수를 받은 사람의 아이디 : " +maxName );
System.out.println("최소점수 : " + minScore);
System.out.println("최소점수를 받은 사람의 아이디 : " +minName );
}
}
Q10
더보기
package check;
import java.util.*;
import java.util.TreeSet;
public class TreeSetExample {
public static void main(String[] args) {
TreeSet<Student1> treeSet = new TreeSet<Student1>();
treeSet.add(new Student1("blue", 96));
treeSet.add(new Student1("hong", 86));
treeSet.add(new Student1("white", 92));
Student1 student = treeSet.last();
System.out.println("최고점수: " + student.score);
System.out.println("최고점수를 받은 아이디: " + student.id);
student = treeSet.first();
System.out.println("최저점수: " + student.score);
System.out.println("최저점수를 받은 아이디: " + student.id);
}
}
class Student1 implements Comparable<Student1>{
public String id;
public int score;
public Student1(String id, int score) {
this.id = id;
this.score = score;
}
@Override
public int compareTo(Student1 o) {
return score - o.score;
}
}
'Java > Study' 카테고리의 다른 글
Ch16 스트림과 병렬처리 (0) | 2021.04.09 |
---|---|
Ch13 제네릭 (0) | 2021.04.02 |
Ch15 컬렉션 프레임워크 (0) | 2021.03.31 |
Ch12. 멀티스레드 (0) | 2021.03.29 |
Ch11 확인문제 (0) | 2021.03.29 |