使用Comparable和Comparator比较泛型 [英] Comparing generic types using Comparable and Comparator

查看:138
本文介绍了使用Comparable和Comparator比较泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了难以调试的头痛。我试图比较两个通用值,以便我可以根据值将它们排序到数组中。这是我第一次使用Comparable和Comparator接口,所以围绕这些问题的任何其他建议都会很棒。



这是我的课程设置:

  public class SVStore< K,V扩展了Comparable< V>>实现对< K,V>,Iterable< K>,Comparable& V< V> {

put()方法:

<$ (K键,V值){

SVData< K,V>选项卡[] =表;
for(int i = 0; i< table.length - 1; i ++){
if(value.compareTo(tab [i] .dataValue)< = 0){
int index = i;
for(int j = index; j< size - 1; j ++){
tab [j + 1] = tab [j];
}
}
tab [i] .setDataKey(key);
tab [i] .setDataValue(value);
size ++;
}
返回值;
}

这些是我试图实现的compareTo()和比较方法。 / p>

  @Override 
public int compareTo(V t){
return compare(t,this);

$ b @Override
public int compare(V t,V t1){
if(t.equals(t1)){
return 0 ;
} else if(t return -1;
} else {
return 1;
}
}

我遇到的第一个问题是在compareTo ()方法,它以this为中心。该错误说必需:V,V
找到:V,SVStore。我知道答案不是将this转换为V.我如何将它与数组索引中的V进行比较?



我遇到的第二个问题是。 ..} else if(t

我认为所有需要的代码都在那里。我尽量保持这些尽可能简明,但如果有任何额外的代码需要,我很乐意提供。欢呼!

解决方案

感谢Chrylis,Catalin Pol和Louis Wasserman的共同努力,我学会了我需要纠正我的课堂到:

  public class SortedValueStore< K,V extends Comparable< ;? super V>>实现PairStore187< K,V> ;, Iterable< K> {b $ b  

我不再需要写我的自己的compareTo()方法。



这个技巧。我对Java比较陌生,所以花了我一段时间才得到每个人都在说什么,但每个人都坚持不懈地努力。感谢所有人的帮助!


I've run into a headache I'm having difficulty debugging. I am trying to compare two generic values so I can insertion sort them according to values into an array. This is my first time working with the Comparable and Comparator interfaces so any additional advice surrounding these issues would be great.

This is how my class is set up:

public class SVStore<K, V extends Comparable<V>> implements Pairs<K, V>, Iterable<K>, Comparable<V>, Comparator<V> {

The put() method:

@Override
public V put(K key, V value) {

    SVData<K, V> tab[] = table;
    for (int i = 0; i < table.length - 1; i++) {
        if (value.compareTo(tab[i].dataValue) <= 0) {
            int index = i;
            for( int j = index; j < size - 1; j++){
                tab[j + 1] = tab[j];
            }
        }
        tab[i].setDataKey(key);
        tab[i].setDataValue(value);
        size++;
    }
    return value;
}

These are the compareTo() and compare methods I am trying to implement.

@Override
public int compareTo(V t) {
 return compare(t, this);
 }

@Override
public int compare(V t, V t1) {
    if (t.equals(t1)){
        return 0;
    } else if (t < t1){
        return -1;
    } else {
        return 1;
    }
}

The first issue I am running into is in the compareTo() method and it is centering around "this". The error says "required: V,V found: V,SVStore". I know the answer is not to cast "this" to V. How do I compare it to the V in that array index?

The second issue I am having is... } else if (t < t1){ in the compareTo() method. The error is "bad operand types for binary operator '<' first type: V second type: V". If it is recognizing both as V for both types why is it a bad operand?

I think all the code needed is there. I try to keep these as concise as possible, but if there's any additional code someone needs, I'm happy to provide it. Cheers!

解决方案

Thanks to the combined efforts of Chrylis, Catalin Pol, and Louis Wasserman, I learned I needed to correct my class to:

public class SortedValueStore<K, V extends Comparable<? super V>> implements PairStore187<K, V>, Iterable<K>, {

I no longer needed to write my own compareTo() method.

This did the trick. I'm relatively new to java so it took me a while to get what everyone was saying, but everyone was persistent with their efforts. Thanks to all for the help!

这篇关于使用Comparable和Comparator比较泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆