Sure, here’s the translation: Java int[] comparator

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

Java int[] comparator

问题

尝试为int[]编写自定义比较器,代码如下所示:

public class ArrayComparator implements Comparator<int[]>{
    @Override
    public int compare(int[] a, int[] b){
        return a[1] - b[1];
    }
}

但是在a = [-2147483646,-2147483645],[2147483646,2147483647]b = [-2147483646,-2147483645],[2147483646,2147483647]这种情况下是不正确的。我应该如何修改我的代码?

英文:

Trying to write a custom comparator for int[] as follows:

public class arrayComparator implements Comparator&lt;int[]&gt;{
    @Override
    public int compare(int[] a, int[] b){
        return a[1] - b[1];
    }
}

But this is not correct for the case where a = [-2147483646,-2147483645],[2147483646,2147483647], b = [-2147483646,-2147483645],[2147483646,2147483647]. How should I revise my code?

答案1

得分: 7

为了避免在比较int值时出现整数溢出问题,我们应该始终使用内置的Integer.compare(int,int)来比较自然顺序。

所以将代码中的:

return a[1] - b[1];

改为:

return Integer.compare(a[1], b[1]);

另外,由于Comparator是一个函数式接口,我们可以通过lambda表达式和/或方法引用来提供其实现:

Comparator<int[]> arrayComparator1 = (a, b) -> Integer.compare(a[1], b[1]);
Comparator<int[]> arrayComparator2 = Comparator.comparingInt(arr -> arr[1]);

除了主要问题之外,Java的普遍接受的编码风格是非原始类型的名称应以大写字母开头,例如StringArrayList。所以将arrayComparator改为ArrayComparator

英文:

Precisely to avoid that kind of problems with integer overflow, to compare int values with natural order we should always use built-in Integer.compare(int,int).

So change

return a[1] - b[1];

into

return  Integer.compare(a[1], b[1]);

BTW since Comparator is functional interface we can provide its implementation via lambdas and/or method-references:

Comparator&lt;int[]&gt; arrayComparator1 = (a, b) -&gt; Integer.compare(a[1], b[1]);
Comparator&lt;int[]&gt; arrayComparator2 = Comparator.comparingInt(arr -&gt; arr[1]);

Aside from main question, generally accepted coding style for Java is that names of non-primitive-types should start with uppercase like String ArrayList. So change arrayComparator into ArrayComparator.

答案2

得分: 1

以下是翻译好的部分:

public class arrayComparator implements Comparator<int[]> {
    @Override
    public int compare(int[] a, int[] b) {
        return a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : 0;
    }
}

public class arrayComparator implements Comparator<int[]> {
    @Override
    public int compare(int[] a, int[] b) {
        if (a[1] > b[1]) {
            return 1;
        }
        if (a[1] < b[1]) {
            return -1;
        }
        return 0;
    }
}

public class arrayComparator implements Comparator<int[]> {
    @Override
    public int compare(int[] a, int[] b) {
        return Integer.compare(a[1], b[1]);
    }
}
英文:

Since you're learning about this, here are several ways of doing it.

public arrayComparator implements Comparator&lt;int[]&gt;{
    @Override
    public int compare(int[] a, int[] b){
        return a[1] &gt; b[1] ? 1 : a[1] &lt; b[1] ? -1 : 0;
    }
}

Or

public arrayComparator implements Comparator&lt;int[]&gt;{
    @Override
    public int compare(int[] a, int[] b){
        
         if (a[1] &gt; b[1]) {
              return 1;
         }
         if (a[1] &lt; b[1]) {
             return -1;
         }
         return 0;
    }
}

Or

public arrayComparator implements Comparator&lt;int[]&gt;{
    @Override
    public int compare(int[] a, int[] b){
        return Integer.compare(a[1], b[1]);
    }
}
   

</details>



# 答案3
**得分**: 0

更简单的写法:

```java
Comparator<int[]> intArrayComparator = (a, b) -> Integer.compare(a[1], b[1]);

例如,可以这样使用:

int[][] arr = ....;
Arrays.sort(arr, intArrayComparator);

无需为此创建显式命名的自定义类。

英文:

Even simpler:

Comparator&lt;int[]&gt; intArrayComparator = (a, b) -&gt; Integer.compare(a[1], b[1]);

For example, to be used as:

int[][] arr = ....;
Arrays.sort(arr, intArrayComparator);

There's no need to make an explicitly named custom class for this.

huangapple
  • 本文由 发表于 2020年10月4日 22:48:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64196074.html
匿名

发表评论

匿名网友

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

确定