Class Case Exception
Class Case Exception
import java.util.Arrays;
class Burger{
String name;
int price;
Burger(String name, int price){}
}
public class Test01 {
public static void main(String[] args) {
Burger[] burgers = new Burger[3];
burgers[0] = new Burger("1957",6000);
burgers[1] = new Burger("๋๋ธ๋ถ๊ณ ๊ธฐ",7000);
burgers[2] = new Burger("ํ์ฐ๋ฒ๊ฑฐ", 8000);
Arrays.sort(burgers); //Exception occurs
}
}
์ ์ฝ๋๋ฅผ ์คํ ์ํค๋ฉด ClassCaseException์ด ๋ฐ์ํ๋ค
์ด๋ Arrays.sort์ ๊ธฐ์ค์ ์ธ comparable์ ๋ฒ๊ฑฐ ๊ฐ์ฒด (name,int)๋ฅผ ์ ๋ ฌํ๋ ๊ธฐ์ค์ ์ด ์๊ธฐ ๋๋ฌธ์ด๋ค. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ comparable class๋ฅผ implements ํด์ compareTo ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํด์ค์ผํ๋ค.
๐คฆ๐ปโ๏ธ CompareTo Method
return values: ์์, 0 , ์์
์์๊ฐ ๋ฆฌํด: ๋ด๊ฐ ๋น๊ต๋์๋ณด๋ค ์์ ์๋ค
0 ๋ฆฌํด: ๋น๊ต ๋์๊ณผ ์๋ฆฌ๋ฅผ ๋ฐ๊พธ์ง ์๊ฒ ๋ค (์ด๋ x)
์์๊ฐ ๋ฆฌํด: ๋ด๊ฐ ๋น๊ต๋์๋ณด๋ค ๋ค์ ์๋ค
CompareTo๋ฅผ ํ์ฉ
public class Student implements Comparable<Student>{
int no, score;
public int getNo() {return this.no;}
public void setNo(int no) {this.no = no;}
public int getScore() {return this.score;}
public void setScore(int score) {this.score = score;}
@Override
public String toString() {
return "Student [no=" + no + ", score=" + score + "]";
}
/* ์์๊ฐ์ด ๋ฆฌํด์ผ ๊ฒฝ์ฐ ๊ธฐ์ค์ ์ด ๋น๊ต๋์์ ์์ ์์น
* ์์๊ฐ์ด ๋ฆฌํด์ผ ๊ฒฝ์ฐ ๊ธฐ์ค์ ์ด ๋น๊ต๋์์ ๋ค์ ์์น
* 0์ผ ๊ฒฝ์ฐ ์ด๋ ์์*/
@Override
public int compareTo(Student o) {
return this.no - o.no; //์ค๋ฆ์ฐจ์
}
CompareTo
1. ๋ด ๋ฒํธ(this.no): 7๋ฒ
2. ๋น๊ต ๋์ ๋ฒํธ(o.no): 3๋ฒ //o๋ ํ๋ผ๋ฏธํฐ ์์ ๋ค์ด๊ฐ ๊ฐ์ฒด์
→ this.no - o.no >0. ์์๋๊น ๋ด๊ฐ ๋น๊ต ๋์๋ณด๋ค ๋ค์ ์์→์ด๋
1. ๋ด๋ฒํธ(this.no):3๋ฒ
2. ๋น๊ต ๋์ ๋ฒํธ(o.no): 7๋ฒ → this.no - o.no <0 : ์์๋๊น ์์ผ๋ก ์ด๋
๐คฆ๐ปโ๏ธ ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ๋ค๋ฉด return๊ฐ์ o.no - this.no; ๋ก ์ฃผ๋ฉด ๋๋ค
ํ ์คํธ ํด๋ณด๋ฉด,
import java.util.Arrays;
public class Test02 {
public static void main(String[] args) {
Student[] arr = {
new Student(2,14),
new Student(1,50),
new Student(4,35),
new Student(3, 90)
};
System.out.println(Arrays.toString(arr));
Arrays.sort(arr);//CompareTo๋ฅผ ์ค๋ฒ๋ผ์ด๋ํด์ ๊ฐ๋ฅ
System.out.println(Arrays.toString(arr));
}
}
Sort ๊ฐ ์๋์ด์๋ ์ถ๋ ฅ๊ฐ:
[Student [no=2, score=14], Student [no=1, score=50], Student [no=4, score=35], Student [no=3, score=90]]
์ค๋ฒ๋ผ์ด๋ฉ๋ Sort๋ฅผ ์ด์ฉํ ์ถ๋ ฅ๊ฐ:
[Student [no=1, score=50], Student [no=2, score=14], Student [no=3, score=90], Student [no=4, score=35]]
Comparable ์ธํฐํ์ด์ค ํ์ฉ
Comparator ์ธํฐํ์ด์ค ๊ตฌํ
private static class StudentComparator implements Comparator <Student>{
@Override
public int compare(Student o1, Student o2) {
return o2.no - o1.no; //๋ด๋ฆผ์ฐจ์
}
}
//์ ์ ์์ผ๋ก ์ค๋ฆ์ฐจ์, ์ ์๊ฐ ๊ฐ์ผ๋ฉด ๋ฒํธ๋ก ๋ด๋ฆผ์ฐจ์์ผ๋ก ๊ธฐ์ค์
//ํ๊ฐ์ง ์ด์์ผ๋ก ๋ ๋:
private static class StudentComparator2 implements Comparator <Student>{
@Override
public int compare(Student o1, Student o2) {
if(o2.score==o1.score) return o2.no-o1.no;//๋ด๋ฆผ์ฐจ์
else return o1.score - o2.score ; //์ค๋ฆ์ฐจ์
}
}
๋ฉ์ธ ๋ฉ์๋:
public static void main(String[] args) {
Student[] arr = {
new Student(2,14),
new Student(1,50),
new Student(4,35),
new Student(5,50), //์ ์๊ฐ ๊ฐ์ ๋
new Student(3, 90)
};
// Student Comparator๊ฐ ์ ๋ ฌ์ ๊ธฐ์ค์ด ๋๋ค
// Arrays.sort(arr, new StudentComparator1());
// System.out.println(Arrays.toString(arr));
Arrays.sort(arr, new StudentComparator2());
System.out.println(Arrays.toString(arr));
}
์ต๋ช ํด๋์ค ์ด์ฉ
private ํด๋์ค๊ฐ ์๋ ์ต๋ช ํด๋์ค๋ก ๋ง๋ค๋์๋ new ํค์๋ ๋ค์์๋ ์์ ํด๋์ค๊ฐ ์์ผํ๋ค
Arrays.sort(arr, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2){
return o1.score-o2.score;
}
});
System.out.println(Arrays.toString(arr));
๋๋ค์ ์ด์ฉ
๐คฆ๐ปโ๏ธ @FunctionalInterface: ์ธํฐํ์ด์ค ์์ ๋ฉ์๋๊ฐ ํ๋๋ง ์๋ ๊ฒฝ์ฐ → ๋๋ค์ ์ฐ๊ด์ง์ด์ ์๊ฐํ์
- ๋๋ค ํด๋์ค์์ ์ธํฐํ์ด์ค์ ์ ์ธ๋ฌธ ๋นผ๊ณ (ํ๋๋ฐ์ ์์ด์) ๋ฉ์๋ ์ด๋ฆ์ ์ธ ํ์๊ฐ ์๋ค.
- ๋ฐํ ํ์ ์ญ์ ์ธํฐํ์ด์ค๊ฐ ์๊ธฐ ๋๋ฌธ์ ํ์ ์๋ค.
- ๋ํ, Student ์ญ์ ์ถ๋ก ์ด ๊ฐ๋ฅํ๋ค (arr๋ฐฐ์ด์ ์ด๋ฏธ Student ๊ฐ ๋ค์ด๊ฐ ์์ด์ → Student ํ์ ๋ ๋น ์ ธ๋ ๋๋ค)
- ํ๋ฌธ์ฅ ์ผ ๋, ์ค๊ดํธ ์๋ต ๊ฐ๋ฅํ์ง๋ง, ๋ฆฌํด์ ๋ฐ๋์ ์ ๊ฑฐํด์ผํจ
Arrays.sort(arr, (o1, o1)-> o1.score - o2.score);
System.out.println(Arrays.toString(arr));