回答編集履歴
1
Comparableの実装例
test
CHANGED
@@ -23,3 +23,53 @@
|
|
23
23
|
Collections.reverseOrder()の代わりにjava.util.Comparatorの実装クラス(匿名クラスで良いです)
|
24
24
|
|
25
25
|
を指定する必要があります。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
Comparatorの実装が提示済みなので、Comparableの例を追記
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
```java
|
36
|
+
|
37
|
+
public class MyData implements Comparable<MyData> {
|
38
|
+
|
39
|
+
public int index;
|
40
|
+
|
41
|
+
public double data;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
MyData(int index, double data) {
|
46
|
+
|
47
|
+
this.index = index;
|
48
|
+
|
49
|
+
this.data = data;
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
public String toString() {
|
56
|
+
|
57
|
+
return "[" + index + "] = " + data;
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
@Override
|
64
|
+
|
65
|
+
public int compareTo(MyData o) {
|
66
|
+
|
67
|
+
if(this.index == o.index) return 0;
|
68
|
+
|
69
|
+
return this.index > o.index ? 1 : -1;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
```
|