英文:
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<int[]>{
@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的普遍接受的编码风格是非原始类型的名称应以大写字母开头,例如String
、ArrayList
。所以将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<int[]> arrayComparator1 = (a, b) -> Integer.compare(a[1], b[1]);
Comparator<int[]> arrayComparator2 = Comparator.comparingInt(arr -> 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<int[]>{
@Override
public int compare(int[] a, int[] b){
return a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : 0;
}
}
Or
public 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;
}
}
Or
public arrayComparator implements Comparator<int[]>{
@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<int[]> intArrayComparator = (a, b) -> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论