我有一个Point
实现Comparable
. 我想要做Point f = arr[first];
现在,我读到使用原始类型Comparable
等是不好的。那么,如果我使用arr[]
类型Comparable<Point>[]
而不是Comparable[]
我会做错什么?
这是被盗的代码,我喜欢它,我偷了它。
private static void sort(Comparable[] a, Point compPoint, int lo, int hi) {
if (hi <= lo)
return;
int lt = lo;
int gt = hi;
int i = lo;
int count = 0;
Comparator comp = compPoint.SLOPE_ORDER;
Comparable v = a[lo];
ArrayList<Point> line = new ArrayList<Point>();
line.add(compPoint);
while (i <= gt) {
int cmp = comp.compare(a[i], v);
if (cmp < 0)
exch(a, lt++, i++);
else if (cmp > 0)
exch(a, i, gt--);
else {
count++;
line.add((Point) a[i]);
i++;
}
}
if (count >= 3) {
Collections.sort(line, new Comparator<Point>() {
public int compare(Point v, Point w) {
return v.compareTo(w);
}
});
for (int j = 0; j < line.size(); j++) {
if (j == line.size() - 1)
StdOut.println(line.get(j).toString());
else
StdOut.print(line.get(j).toString()
+ " -> ");
}
line.get(0).drawTo(line.get(line.size() - 1));
}
sort(a, compPoint, lo, lt - 1);
sort(a, compPoint, gt + 1, hi);
}
private static void exch(Comparable[] a, int v, int w) {
Comparable tmp = a[v];
a[v] = a[w];
a[w] = tmp;
}
我想知道是否有比使用原始类型 Comparable 更好的方法。