I have a Point
class which implements Comparable
.
I want to do Point f = arr[first];
Now , I have read that using raw type Comparable
etc is bad.
So , If i make arr[]
of type Comparable<Point>[]
instead of Comparable[]
What will i be doing wrong ?
This is stolen code , i loved it and i stole it.
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;
}
I want to know if there is a better way than having raw type Comparable.