比较 Java 中的可比较数组
我想比较一系列可比较的东西。最简单的方法如下(详细信息未显示):
public class ArrayComparable implements Comparable<ArrayComparable>{
ArrayList<Comparable<?>> list = new ArrayList<Comparable<?>>();
@Override
public int compareTo(ArrayComparable ac) {
Iterator<Comparable<?>> itr = ac.list.iterator();
for(Comparable<?> l : list) {
Comparable<?> itrNext = itr.next();
if(itrNext.getClass() == l.getClass()) {
if(itrNext.compareTo(l)) {
//something
} else {
//other stuff
}
} else {
//some other thing
}
}
}
当然,这里的问题是 itrNext.compareTo(l)
中的 compareTo
将无法工作并给出错误: Comparable
我明白为什么(就方法而言,我可能会将苹果与橙子进行比较)。另一方面,我知道我不是,因为我在比较事物之前先检查它们的类别。
那么有什么办法可以让我完成这项工作吗?不要担心比较任何可比较数组的合理性,因为我有充分的理由这样做。
编辑-那么我为什么要做这样的事情。假设我想要一个可比较的数组,并且我不关心每个索引中包含什么,只要类型对应,并且可以比较它们即可。然后我可以在这些数组之间进行一般的字典顺序比较。这样我就不必为 (int,int)
和 (int, string)
以及 (string, double, string)< 编写比较/code> 或任何你需要的东西。我只写一个,只要我确保类型匹配(并且我可以),我就可以开始了。
I want to compare an array of comparables. The simplest way seems the following (details not shown):
public class ArrayComparable implements Comparable<ArrayComparable>{
ArrayList<Comparable<?>> list = new ArrayList<Comparable<?>>();
@Override
public int compareTo(ArrayComparable ac) {
Iterator<Comparable<?>> itr = ac.list.iterator();
for(Comparable<?> l : list) {
Comparable<?> itrNext = itr.next();
if(itrNext.getClass() == l.getClass()) {
if(itrNext.compareTo(l)) {
//something
} else {
//other stuff
}
} else {
//some other thing
}
}
}
Of course the problem here is that the compareTo
as in itrNext.compareTo(l)
will not work giving the error: The method compareTo(capture#6-of ?) in the type Comparable<capture#6-of ?> is not applicable for the arguments (Comparable<capture#7-of ?>)
which I understand why (as far as the method is concerned I might be comparing apples to oranges). On the other hand, I know I am not as I check for the class of things before comparing them.
So is there a way I can make this work? Don't worry about the sanity of comparing arrays of any comparables, as I have a good reason why I want to do that.
EDIT- SO why would I want to do something like this. Say I wanted to have an array of comparables, and I didn't care what was contained in each index, as long as the types corresponded, and they could be compared. Then I could do a general lexicographical compare between these arrays. This way I don't have to write a comparable for (int,int)
and (int, string)
, and (string, double, string)
or whatever you need. I just write one, and as long as I make sure that the types match (and I can), I am good to go.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在当前使用
Comparable
的地方,使用原始类型Comparable
应该可以。实际上,如果您愿意,您可以在一个地方执行此操作:Using the raw type
Comparable
wherever you're currently usingComparable<?>
should work. Actually, you could just do that in one place if you want:将 ArrayComparable 设为泛型类,以便您可以正确参数化泛型,而不是到处使用
。哦,您也可以实现
Iterable
。Make
ArrayComparable
a generic class so that you can properly parameterize the generics rather than using<?>
everywhere. Oh, and you might as well implementIterable
as well.试试这个:
Try this:
对此的一个很好的答案是:
A good answer to this would be: