0

I want to define a generic class ComparableList<> that extend ArrayList and implements Comparable interfaces, such that two objects of type ComparableList can be compared using the compareTo method. The compareTo should perform a lexicographic comparison. Here's my code:

class ComparableList <T extends Comparable<T>> extends ArrayList implements Comparable<ComparableList>{
@Override
    public int compareTo(ComparableList o){
        Iterator citer = this.iterator();
        Iterator oiter = o.iterator();
        while (citer.hasNext() && oiter.hasNext()){
            if (citer.next() > oiter.next()){
                return 1;
            }else if (citer.next() < oiter.next()){
                return -1;
            }else {
                if (!citer.hasNext()){
                    return -1;
                }
                if(!oiter.hasNext()){
                    return 1;
                }
            }
        }
        return 0;
}

}

and I got error messages like this:

TCL.java:11: error: bad operand types for binary operator '>'
            if (citer.next() > oiter.next()){
                             ^
first type:  Object
second type: Object
TCL.java:13: error: bad operand types for binary operator '<'
            }else if (citer.next() < oiter.next()){
                                   ^
first type:  Object 
second type: Object

I thought it should be a ComparableList but not an Object. Can anyone tell me the reason?

3 Answers 3

1

You need to compare the objects using Comparable.comapreTo() (that's why you have <T extends Comparable<T> there). You need to first check for nulls on either side.

Also, each call to Iterator.next() iterates to next element, you don't want to call it twice in one loop iteration - store the items at the loop start then use the stored values.

Sign up to request clarification or add additional context in comments.

2 Comments

I modified the code to Iterator i = iter.next(), o = oiter.next(); but the compiler said Object cannot be converted to Iterator Seems like it is still not a ComparableList type
Why would you expect iter.next() to return an Iterator? It returns the next item - that one has type T.
1

Comparable doesn't override the > and < operators (nothing can). Since your T implements Comparable, use compareTo:

int result = citer.next().compareTo(oiter.next());
if (result != 0) {
    return result;
} else {
    if (citer.hasNext()) {
        return -1;
    }
    if (oiter.hasNext()) {
        return 1;
    }
}

Note that that also calls next only once per iteration, since next advanced the iterator.

1 Comment

I modified my code but get method compareTo(Object) error. Seems like it is still an Object, not a ComparableList type I want
1

Each element in your ComparableList is of type T extends Comparable<T>, for sure the binary operator is not available for it (Java doesn't have operator overloading), but since it extends Comparable, you have compareTo to be used as replacement for < and >. Use it instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.