【问题标题】:Comparing two private variables stored by custom objects that implements 'Comparable'比较实现“可比”的自定义对象存储的两个私有变量
【发布时间】:2014-12-05 06:47:31
【问题描述】:

我试图通过打印出最前面的三个单词来找到一个段落并找到它的“含义”。去掉所有语法单词和空格后,我使用 Hashmap 来计算每个单词的出现次数。然后因为我不知道更好的方法,我只是创建了我自己的小自定义对象来存储单词、键和出现值、值,就像 Hashmap 一样,但是我的老师建议实现 Comparable,但我遇到了一个问题。我遇到两个问题,一个在我“修复”另一个时出现。问题在于 Pair 类中的 compareTo 函数和另一个类中的 Sort() 函数。当我让 compareTo 函数返回 int 时,我得到一个“对不能转换为布尔值”。而且它不允许我使用 compareTo 函数。

compareTo() 我也知道 compareTo 应该将 int 返回到 Override,但是当我跳到这里寻求帮助时,代码就是这样。类原型也是

    public class Pair implements Comparable<Pair>{
    ///.... variables, getters, setters, etc.

    @Override
    public boolean compareTo(Pair o) {    // ERROR HERE

    boolean z = false;

    if (this.getValue() > o.getValue()) {
        z = true;
    }
    else{
        z = false;
    }
    return false; 
}

以及使用 Pair 对象的 sort/countwords 函数。

public Pair[] sort(Pair[] arr) {
    int max;
    for (int i = 0; i < arr.length; i++) {
        // Assume first element is min
        max = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j],compareTo(arr[max])  == true){ //ERROR HERE
                max = j;
            }
        }
        if (max != i) {
            final Pair temp = arr[i];
            arr[i] = arr[max];
            arr[max] = temp;
        }
        System.out.println(arr[i].getKey());// I print the in ascending order
    }
    return arr; 
}

public void countWords() {
    Map<String, Integer> m = new HashMap(); // set up a Hashmap to get the occurance of the words
    for (String a : dataList) {
        Integer freq = m.get(a);
        m.put(a, (freq == null) ? 1 : freq + 1);
    }
    //create and fill array of custom Pair (key, value) objects, so it becomes sortable
    int count = 0;
    Pair[] pairs = new Pair[m.size()];
    for (String key : m.keySet()) {
        Pair pair = new Pair(key, m.get(key));
        pairs[count] = pair;
        count++;
    }
    Pair[] sortedPairs = sort(pairs);
}

【问题讨论】:

    标签: java sorting comparable


    【解决方案1】:

    compareTo() 必须返回一个符合以下条件的 int:

    如果当前对象的值小于另一个,则返回 -1,如果相等则返回 0,如果大于则返回 1。

    将您的方法更改为(假设 getValue() 返回int):

    @Override
    public int compareTo(Pair o) {
           return (this.getValue() < o.getValue()) ? -1 : (this.getValue() > o.getValue()) ? 1 : 0 ;
        }
    

    请注意,如果getValue() 返回其他对象类型,您可以使用该对象的默认实现compareTo()。因此,如果getValue() 将返回例如BigDecimalInteger 对象等,您可以使用return this.getValue().compareTo(o.getValue());

    然后,在使用方法的时候,换行

    if (arr[j],compareTo(arr[max])  == true)
    

    if (arr[j].compareTo(arr[max])  == 0 ) //they are equal
    

    【讨论】:

      【解决方案2】:

      compareTo() 必须返回一个int。它返回 -1 (object1 object2)。在您的代码中,您可以像这样使用它:

      if ( pairA.compareTo(pairB) > 0 ) // if pairA > pairB
      

      【讨论】:

        【解决方案3】:

        compareTo 必须返回一个 int,否则你实际上并没有实现接口中定义的抽象方法。

        根据 Pair 类中的 getValue() 方法返回的类型,您很可能将方法简单地定义为:

        public int compareTo(Pair o) {    
        
           return getValue().compareTo(o.getValue());
        }
        

        【讨论】:

          猜你喜欢
          • 2019-01-06
          • 2023-03-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多