简体   繁体   English

如何定义通用的可比较数组类?

[英]How to define a generic comparable array class?

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. 我想定义一个通用类ComparableList <>,该类扩展ArrayList并实现Comparable接口,以便可以使用compareTo方法比较ComparableList类型的两个对象。 The compareTo should perform a lexicographic comparison. compareTo应该执行字典比较。 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. 我认为这应该是ComparableList,而不是Object。 Can anyone tell me the reason? 谁能告诉我原因?

You need to compare the objects using Comparable.comapreTo() (that's why you have <T extends Comparable<T> there). 您需要使用Comparable.comapreTo()比较这些对象(这就是为什么您在其中<T extends Comparable<T> )。 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. 另外,每次对Iterator.next()调用都会迭代到下一个元素,您不想在一个循环迭代中两次调用它-在循环开始时存储项目,然后使用存储的值。

Comparable doesn't override the > and < operators (nothing can). Comparable不会覆盖><运算符(什么也不能)。 Since your T implements Comparable , use compareTo : 由于您的T实现了Comparable ,请使用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. 请注意,还要求next每次迭代只有一次 ,因为next先进的迭代器。

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 >. 您的ComparableList中的每个元素的类型都是T extends Comparable<T> ,以确保二进制运算符对此不可用(Java没有运算符重载),但是由于它扩展了Comparable ,因此您可以使用compareTo来代替<和>。 Use it instead. 改用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM