• Class Case Exception

    2022. 5. 17.

    by. JuneBee

    728x90
    ๋ฐ˜์‘ํ˜•

    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: ์ธํ„ฐํŽ˜์ด์Šค ์•ˆ์— ๋ฉ”์„œ๋“œ๊ฐ€ ํ•˜๋‚˜๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ โ†’ ๋žŒ๋‹ค์™€ ์—ฐ๊ด€์ง€์–ด์„œ ์ƒ๊ฐํ•˜์ž
    1. ๋žŒ๋‹ค ํด๋ž˜์Šค์—์„œ ์ธํ„ฐํŽ˜์ด์Šค์˜ ์„ ์–ธ๋ฌธ ๋นผ๊ณ (ํ•˜๋‚˜๋ฐ–์— ์—†์–ด์„œ) ๋ฉ”์„œ๋“œ ์ด๋ฆ„์„ ์“ธ ํ•„์š”๊ฐ€ ์—†๋‹ค.
    2. ๋ฐ˜ํ™˜ ํƒ€์ž… ์—ญ์‹œ ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š” ์—†๋‹ค.
    3. ๋˜ํ•œ, Student ์—ญ์‹œ ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•˜๋‹ค (arr๋ฐฐ์—ด์— ์ด๋ฏธ Student ๊ฐ€ ๋“ค์–ด๊ฐ€ ์žˆ์–ด์„œ โ†’ Student ํƒ€์ž…๋„ ๋น ์ ธ๋„ ๋œ๋‹ค)
    4. ํ•œ๋ฌธ์žฅ ์ผ ๋•Œ, ์ค‘๊ด„ํ˜ธ ์ƒ๋žต ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ๋ฆฌํ„ด์„ ๋ฐ˜๋“œ์‹œ ์ œ๊ฑฐํ•ด์•ผํ•จ
    Arrays.sort(arr, (o1, o1)-> o1.score - o2.score);
    System.out.println(Arrays.toString(arr));
    728x90
    ๋ฐ˜์‘ํ˜•

    '๐Ÿ““ STUDY > JAVA' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    ์ถ”์ƒํ™”  (0) 2022.05.17
    ๊ฐ์ฒด ์ง€ํ–ฅ ํ”„๋กœ๊ทธ๋ž˜๋ฐ  (1) 2022.05.17
    JAVA Basics  (0) 2022.05.12
    Variables  (0) 2021.07.15
    ๋‹ค์ง  (0) 2021.07.15

    ๋Œ“๊ธ€