- Notifications
You must be signed in to change notification settings - Fork 846
/
Copy path9.java
46 lines (35 loc) · 1001 Bytes
/
9.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
importjava.util.*;
classFruitimplementsComparable<Fruit> {
privateStringname;
privateintscore;
publicFruit(Stringname, intscore) {
this.name = name;
this.score = score;
}
publicStringgetName() {
returnthis.name;
}
publicintgetScore() {
returnthis.score;
}
// 정렬 기준은 '점수가 낮은 순서'
@Override
publicintcompareTo(Fruitother) {
if (this.score < other.score) {
return -1;
}
return1;
}
}
publicclassMain {
publicstaticvoidmain(String[] args) {
List<Fruit> fruits = newArrayList<>();
fruits.add(newFruit("바나나", 2));
fruits.add(newFruit("사과", 5));
fruits.add(newFruit("당근", 3));
Collections.sort(fruits);
for (inti = 0; i < fruits.size(); i++) {
System.out.print("(" + fruits.get(i).getName() + "," + fruits.get(i).getScore() + ") ");
}
}
}