Somjit
Somjit

Reputation: 2772

Comparable< type > array VS Comparable array

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.

Upvotes: 2

Views: 4736

Answers (3)

Bohemian
Bohemian

Reputation: 425198

As it is now, you could call it like this:

String[] array = {"a", "b"};
sort(array, point, 1, 2);

Clearly this is ridiculous. Comparing a String with a point is, well pointless.

It seems that the code in the method would work for any comparable type. Consider changing the signature to thus:

private static <T extends Comparable<T>> void sort(T[] a, T comp, int lo, int hi) {

Upvotes: 3

Eric
Eric

Reputation: 97631

Here are a bunch of things you should do:

  • class Point implements Comparable should become class Point implements Comparable<Point>
  • Comparator should become Comparator<Point>
  • Replace Comparable[] with Point[] - if you're going to cast the elements to Points, you may as well declare them as points
  • Replace

    Collections.sort(line, new Comparator<Point>() {
        public int compare(Point v, Point w) {
            return v.compareTo(w);
        }
    });
    

    with the less redundant

    Collections.sort(line);
    

Upvotes: 0

A4L
A4L

Reputation: 17595

There is nothing wrong with refactoring the code to use generics. It seems that this method is used only for Points so I'd go with Point[] instead of Comparable<Point>[]. From what I see you'll have also to refactor the class Point and the compare method of the Comaprator SLOPE_ORDER in it.

If you are permitted, just download all the code and refactor it. If it compiles everything will be fine, then this is the point of java generics: it helps at compile time do nothing wrong. At runtime it's all raw anyways due to type erasure.

Upvotes: 0

Related Questions