Java
자바 클래스 핵심
class Student {
int kor, eng, math;
public Student(int kor, int eng, int math){
this.kor = kor;
this.eng = eng;
this.math = math;
} // 생성자(constructor) 라 불리는 함수
};
// 초기값을 주고 싶을때는 동일한 것을 선언하여서 주면된다
class Student {
int kor, eng, math;
public Student(){
this.kor = 0;
this.eng = 0;
this.math = 0;
}
public Student(int kor, int eng, int math){
this.kor = kor;
this.eng = eng;
this.math = math;
}
};
public class Main {
public static void main(String[] args) {
Student student1 = new Student(90, 80, 90); // 넘어간 값을 사용
System.out.println(student1.kor); // 90
System.out.println(student1.eng); // 80
System.out.println(student1.math); // 90
Student student2 = new Student(); // 값이 넘어가지 않는 생성자를 이용
System.out.println(student2.kor); // 0
System.out.println(student2.eng); // 0
System.out.println(student2.math); // 0
}
}
객체 정렬 @override로 / implements Comparable
Java에서는 custom comparator를 만들어야 합니다. 이 함수는 반환 타입이 꼭 int 이어야 하며, 해당하는 class를 type으로 하는 1개의 인자를 갖고 있어야만 합니다. 가장 일반적인 방법은 class 뒤에 implements Comparable
class Student implements Comparable<Student> {
int kor, eng, math;
public Student(int kor, int eng, int math){
this.kor = kor;
this.eng = eng;
this.math = math;
}
@Override
public int compareTo(Student student) { // 국어 점수 기준 오름차순 정렬
if(this.kor > student.kor)
return 1;
else if(this.kor < student.kor)
return -1;
else
return 0;
}
};
///
@Override
public int compareTo(Student student) { // 국어 점수 기준 오름차순 정렬
return this.kor - student.kor;
}
하지만 Java 8 부터는 굳이 정렬 기준을 class안에 적지 않고, Arrays.sort 사용시 인자로 정렬 함수를 다음과 같이 넣어주는 방식도 가능합니다. 두 인자 값을 순서대로 a, b라 했을 때 국어 점수 기준 오름차순 정렬을 위해서는 a.kor - b.kor를 반환해주면 됩니다. 이는 lambda라는 표현을 사용하여 함수를 간단히 작성이 가능합니다.
Arrays.sort(students, (a, b) -> a.kor - b.kor); // 국어 점수 기준 오름차순 정렬
comparator를 사용한 정렬
// custom comparator를 활용한 정렬
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student a, Student b) { // 키를 기준 오름차순 정렬합니다.
return a.height - b.height;
}
});
comparator를 이용해서 main안에서 정렬 때리기
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
// 학생들의 정보를 나타내는 클래스 선언
class Student {
String name;
int height;
double weight;
public Student(String name, int height, double weight){
this.name = name;
this.height = height;
this.weight = weight;
}
};
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 변수 선언 및 입력:
int n = 5;
Student[] students = new Student[n];
for(int i = 0; i < n; i++) {
String name = sc.next();
int height = sc.nextInt();
double weight = sc.nextDouble();
// Student 객체를 생성해 리스트에 추가합니다.
students[i] = new Student(name, height, weight);
}
// custom comparator를 활용한 정렬
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student a, Student b) { // 이름 기준 오름차순 정렬합니다.
return a.name.compareTo(b.name);
}
});
// 이름순으로 정렬한 결과를 출력합니다.
System.out.println("name");
for (int i = 0; i < n; i++){
System.out.print(students[i].name + " ");
System.out.print(students[i].height + " ");
System.out.printf("%.1f\n", students[i].weight);
}
System.out.println();
// custom comparator를 활용한 정렬
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student a, Student b) { // 키 기준 내림차순 정렬합니다.
return b.height - a.height;
}
});
// 키순으로 정렬한 결과를 출력합니다.
System.out.println("height");
for (int i = 0; i < n; i++){
System.out.print(students[i].name + " ");
System.out.print(students[i].height + " ");
System.out.printf("%.1f\n", students[i].weight);
}
}
}