使用具有自定义比较器的优先队列,根据字符串中的单词数量进行排序。

huangapple go评论80阅读模式
英文:

Using Priority Queue with Custom Comparator to Sort Based on Number of Words in a String?

问题

Comparator<String> whiteSpaces = new Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        int count1 = 0;
        int count2 = 0;
        char c = ' ';
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) == c) {
                count1++;
            }
        }
        for (int i = 0; i < s2.length(); i++) {
            if (s2.charAt(i) == c) {
                count2++;
            }
        }
        return count1 - count2;
    }
};
英文:

I'm trying to use Priority Queue to sort strings based on how many words (white spaces) there are in each string. My code works only in certain scenerios, but not when the amount of white spaces per string is really spread out. I think because my methods aren't comparing them all.

Comparator&lt;String&gt; whiteSpaces = new Comparator&lt;String&gt;() {
        int count1 = 0;
        int count2 = 0;
        char c = &#39; &#39;;
        @Override
        public int compare(String s1, String s2) {
            for(int i = 0; i &lt; s1.length(); i++) {
                if(s1.charAt(i) == c){
                    count1++;
                }
            }
            for(int i = 0; i &lt; s2.length(); i++) {
                if(s2.charAt(i) == c) {
                    count2++;
                }
            }
            return count1 - count2;
        }
    };

Could I modify what I have so that it compares to all the strings added to the priorityqueue? Thanks in advance.

答案1

得分: 1

Comparator无法正常工作,因为您在调用compare()之间没有将count1count2重置为0

实际上,问题在于count1count2是字段。它们应该是局部变量。

count1count2的声明移到方法内部。

Comparator<String> whiteSpaces = new Comparator<String>() {
    char c = ' ';
    @Override
    public int compare(String s1, String s2) {
        int count1 = 0;
        int count2 = 0;
        for(int i = 0; i < s1.length(); i++) {
            if(s1.charAt(i) == c){
                count1++;
            }
        }
        for(int i = 0; i < s2.length(); i++) {
            if(s2.charAt(i) == c) {
                count2++;
            }
        }
        return count1 - count2;
    }
};

其他改进:

  • 字段c应该是privatefinal的,并且命名更合适。

  • 创建一个辅助方法以消除重复的代码。

  • 使用Integer.compare(a, b)来替代减法。

Comparator<String> whiteSpaces = new Comparator<String>() {
    private final char separator = ' ';
    @Override
    public int compare(String s1, String s2) {
        return Integer.compare(countSeparators(s1), countSeparators(s2));
    }
    private int countSeparators(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == separator) {
                count++;
            }
        }
        return count;
    }
};
英文:

The Comparator isn't working because you don't reset count1 and count2 to 0 between calls to compare().

Actually, the problem is that count1 and count2 are fields. They should be local variables.

Move count1 and count2 declarations into the method.

Comparator&lt;String&gt; whiteSpaces = new Comparator&lt;String&gt;() {
    char c = &#39; &#39;;
    @Override
    public int compare(String s1, String s2) {
        int count1 = 0;
        int count2 = 0;
        for(int i = 0; i &lt; s1.length(); i++) {
            if(s1.charAt(i) == c){
                count1++;
            }
        }
        for(int i = 0; i &lt; s2.length(); i++) {
            if(s2.charAt(i) == c) {
                count2++;
            }
        }
        return count1 - count2;
    }
};

Other improvements:

  • Field c should be private and final, and better named.

  • Create a helper method to eliminate repeated code.

  • Use Integer.compare(a, b) instead of subtraction.

<!-- -->

Comparator&lt;String&gt; whiteSpaces = new Comparator&lt;String&gt;() {
    private final char separator = &#39; &#39;;
    @Override
    public int compare(String s1, String s2) {
        return Integer.compare(countSeparators(s1), countSeparators(s2));
    }
    private int countSeparators(String s) {
        int count = 0;
        for (int i = 0; i &lt; s.length(); i++) {
            if (s.charAt(i) == separator) {
                count++;
            }
        }
        return count;
    }
};

huangapple
  • 本文由 发表于 2020年4月8日 04:39:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/61089048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定