你如何去比较两个泛型类?Java泛型比较
class Entry<K,V>
{
protected K key;
protected V value;
public K getKey() { return key; }
public V getValue() { return value; }
public static Comparator KeyComparator = new Comparator()
{
public int compare(Object o1, Object o2)
{
int key1 = ((Entry) o1).getKey();
int key2 = ((Entry) o2).getKey();
if (key1 > key2)
{
return 1;
}
else if (key1 < key2)
{
return -1;
}
else
{
return 0;
}
}
};
}
我得到以下编译错误:
int key1 = ((Entry) o1).getKey();
^
int key2 = ((Entry) o2).getKey();
^
incompatible types
found : java.lang.Object
required: int
K
和V
将是一个圆形的ArrayList实现中使用Integer
秒。有没有更简单的方法(或至少有一个有效)比较像我通常会与int
?
Java不知道,会被用来做什么的通用对象。 – Trick 2009-11-25 08:48:57
你可以发布更多的编译错误 - 包括行号。对于这个问题可能没问题,但一般来说,在寻求帮助时这是有用的信息。 – Chii 2009-11-25 08:53:34