고급JAVA
[JAVA] Comparable 와 Comparator 활용 - 학생 정보 정렬
헤니s
2022. 12. 20. 21:21
< 문제 >
1. 학번, 이름, 국어, 영어, 수학, 총점, 등수로 Student 클래스 생성
2. Student 클래스의 생성자는 학번, 이름, 국어, 영어, 수학을 매개변수로 객체생성한다.(초기화)
3. Student 객체를 List에 저장하여 관리한다. + 등수(rank 구하기)
4. 학번의 오름차순으로 정렬(내부정렬 Comparable 재정의)
5. 총점의 내림차순으로 정렬(외부정렬 Comparator 재정의)
6. 총점이 같으면 이름의 오름차순으로 정렬
▶ Student 클래스 생성 + 내부정렬기준(학번의 오름차순)
class Student implements Comparable<Student> {
int id;
String name;
int kor, eng, math;
int total;
int rank = 0;
public Student(int id, String name, int kor, int eng, int math) {
super();
this.id = id;
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
this.total = kor + eng + math;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
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;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
@Override
public String toString() {
return "Student [학번=" + id + ", 이름=" + name + ", 국어점수=" + kor + ", 영어점수=" + eng
+ ", 수학점수=" + math + ", 총점=" + total + ", 등수=" + rank + "]";
}
//학번의 오름차순... 정렬
@Override
public int compareTo(Student stu) {
if(this.id > stu.getId()) {
return 1;
} else if(this.id < stu.getId()) {
return -1;
} else {return 0;}
}
}
▶ main 에서 ArrayList 에 Student 객체 담기 + 학번의 오름차순 정렬
public static void main(String[] args) {
ArrayList<Student> StudentList = new ArrayList <Student>();
StudentList.add(new Student(1, "홍길동", 80, 90, 100));
StudentList.add(new Student(3, "이길동", 50, 75, 60));
StudentList.add(new Student(4, "박길동", 75, 70, 55));
StudentList.add(new Student(6, "삼길동", 70, 100, 90));
StudentList.add(new Student(5, "오길동", 70, 60, 85));
StudentList.add(new Student(2, "지길동", 95, 65, 100));
System.out.println("정렬전");
for (Student stu : StudentList) {
System.out.println(stu);
}
System.out.println("-----------------------------------------");
System.out.println("학번의 오름차순 정렬");
Collections.sort(StudentList);
for (Student stu : StudentList) {
System.out.println(stu);
}
}
▶ 등수(rank) 구하는 메서드 생성
public void getRank(ArrayList<Student> StudentList) {
for (int i = 0; i < StudentList.size(); i++) {
int rank = 1;
for (int j = 0; j < StudentList.size(); j++) {
if(StudentList.get(i).total < StudentList.get(j).total) {
rank ++;
}
StudentList.get(i).setRank(rank);
}
}
}
-------------------------------------------------------------------------
자신의 등수를 1등으로 놓고 이중 for문으로 total점수 비교
StudentList.get(i)의 총점보다 StudentList.get(j)의 총점이 더 크면
등수를 + 1 하여라
=> setter에서 get(i).setRank(rank) 현재 등수로 변경시켜줌
▶ 외부정렬기준 클래스 생성
- 총점의 역순으로
- 총점이 같으면 이름의 오름차순으로
// 1. 총점의 역순으로 2. 총점이 같으면 이름의 오름차순으로
class totalNameSorting implements Comparator<Student>{
@Override
public int compare(Student stu1, Student stu2) {
if(stu1.getTotal() == stu2.getTotal()) {
return stu1.getName().compareTo(stu2.getName());
//총점이 같으면 이름의 오름차순 (return 1)
} else {
return Integer.compare(stu1.getTotal(),stu2.getTotal()) * -1 ;
//총점이 같지 않다면 총점의 내림차순 (return -1)
}
}
}
▶ main 메서드 실행
public static void main(String[] args) {
ArrayList<Student> StudentList = new ArrayList <Student>();
StudentList.add(new Student(1, "홍길동", 80, 90, 100));
StudentList.add(new Student(3, "이길동", 50, 75, 60));
StudentList.add(new Student(4, "박길동", 75, 70, 55));
StudentList.add(new Student(6, "삼길동", 70, 100, 90));
StudentList.add(new Student(5, "오길동", 70, 60, 85));
StudentList.add(new Student(2, "지길동", 95, 65, 100));
//StudentList에 값을 입력하고 석차 구하기
new StudentTest().getRank(StudentList);
System.out.println("정렬전");
for (Student stu : StudentList) {
System.out.println(stu);
}
//내부정렬기준으로 학번의 오름차순 정렬
System.out.println("-----------------------------------------");
System.out.println("학번의 오름차순 정렬");
Collections.sort(StudentList);
for (Student stu : StudentList) {
System.out.println(stu);
}
System.out.println("-----------------------------------------");
System.out.println("총점의 역순, 총점이 같으면 이름의 오름차순");
Collections.sort(StudentList, new totalNameSorting());
for (Student stu : StudentList) {
System.out.println(stu);
}
}
▶ 전체 코드
더보기
package review.StudentSort_221220;
import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class StudentTest {
public void getRank(ArrayList<Student> StudentList) {
for (int i = 0; i < StudentList.size(); i++) {
int rank = 1;
for (int j = 0; j < StudentList.size(); j++) {
if(StudentList.get(i).total < StudentList.get(j).total) {
rank ++;
}
StudentList.get(i).setRank(rank);
}
}
}
public static void main(String[] args) {
ArrayList<Student> StudentList = new ArrayList <Student>();
StudentList.add(new Student(1, "홍길동", 80, 90, 100));
StudentList.add(new Student(3, "이길동", 50, 75, 60));
StudentList.add(new Student(4, "박길동", 75, 70, 55));
StudentList.add(new Student(6, "삼길동", 70, 100, 90));
StudentList.add(new Student(5, "오길동", 70, 60, 85));
StudentList.add(new Student(2, "지길동", 95, 65, 100));
new StudentTest().getRank(StudentList);
System.out.println("정렬전");
for (Student stu : StudentList) {
System.out.println(stu);
}
//석차구하기...
//for문으로... 나와 남의 총점을 비교하여ㅑ..... setRank로,...
//내부정렬기준으로 학번의 오름차순 정렬
System.out.println("-----------------------------------------");
System.out.println("학번의 오름차순 정렬");
Collections.sort(StudentList);
for (Student stu : StudentList) {
System.out.println(stu);
}
System.out.println("-----------------------------------------");
System.out.println("총점의 역순, 총점이 같으면 이름의 오름차순");
Collections.sort(StudentList, new totalNameSorting());
for (Student stu : StudentList) {
System.out.println(stu);
}
}
}
class Student implements Comparable<Student> {
int id;
String name;
int kor, eng, math;
int total;
int rank = 0;
public Student(int id, String name, int kor, int eng, int math) {
super();
this.id = id;
this.name = name;
this.kor = kor;
this.eng = eng;
this.math = math;
this.total = kor + eng + math;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getKor() {
return kor;
}
public void setKor(int kor) {
this.kor = kor;
}
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;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
@Override
public String toString() {
return "Student [학번=" + id + ", 이름=" + name + ", 국어점수=" + kor + ", 영어점수=" + eng
+ ", 수학점수=" + math + ", 총점=" + total + ", 등수=" + rank + "]";
}
//학번의 오름차순... 정렬
@Override
public int compareTo(Student stu) {
if(this.id > stu.getId()) {
return 1;
} else if(this.id < stu.getId()) {
return -1;
} else {
return 0;
}
}
}
class totalNameSorting implements Comparator<Student>{
// 1. 총점의 역순으로 2. 총점이 같으면 이름의 오름차순으로
@Override
public int compare(Student stu1, Student stu2) {
if(stu1.getTotal() == stu2.getTotal()) {
return stu1.getName().compareTo(stu2.getName());
} else {
return Integer.compare(stu1.getTotal(),stu2.getTotal()) * -1 ;
}
}
}