英文:
java.lang.IllegalArgumentException: Comparison method violates its general contract, only in particular test case
问题
这是我的比较函数:
Arrays.sort(intervals, (a, b) -> {
return (a[0] > b[0]) ? 1 : ((a[0] < b[0]) ? -1 : ((a[1] > b[1]) ? 1 : -1));
});
我认为我已经明确地为每个条件指定了返回值,当a
和b
是大小为2
的数组时,但我不知道为什么在一个测试用例中出现错误,而其他测试用例则正常运行:
java.lang.IllegalArgumentException: Comparison method violates its general contract:
at line 903, java.base/java.utili.TimSort.mergeHi
at line 520, java.base/java.utili.TimSort.mergeAt
at line 448, java.base/java.utili.TimSort.mergeCollapse
at line 245, java.base/java.utili.TimSort.sort
at line 1442, java.base/java.utili.Arrays.sort
at line 3, Solution.merge
at line 54, __DriverSolution__.__helper__
at line 84, __Driver__.main
英文:
Here is my comparator function:
Arrays.sort(intervals, (a, b)->{
return (a[0] > b[0]) ? 1 : ((a[0] < b[0]) ? -1 : ((a[1] > b[1]) ? 1 : -1));
});
I think I've clearly specified return value for each condition in a case when a
and b
are arrays of size 2
, but I don't know why I'm getting this error in a single test case while others run properly:
java.lang.IllegalArgumentException: Comparison method violates its general contract:
at line 903, java.base/java.utili.TimSort.mergeHi
at line 520, java.base/java.utili.TimSort.mergeAt
at line 448, java.base/java.utili.TimSort.mergeCollapse
at line 245, java.base/java.utili.TimSort.sort
at line 1442, java.base/java.utili.Arrays.sort
at line 3, Solution.merge
at line 54, __DriverSolution__.__helper__
at line 84, __Driver__.main
答案1
得分: 1
如果你未能涵盖一个情况,你的代码甚至无法编译。
就目前而言,你的代码可以编译,但它并不满足比较方法的要求。这是一个不同的问题。
以下是一般约定:
- 如果a < b且b < c,则a < c。
- 如果a == b,则compare(a, b) 必须返回0。
- 如果a < b,则b > a。
- 如果你愿意,可以抛出异常。
你已经违反了这个约定:如果我将相同的值传递给你的比较方法,最后一个情况会发生,并且返回-1,这意味着:“a在a下面”,这违反了规则。
将最后一个节点(-1)展开为:(a[1] < b[1]) ? -1 : 0
。
英文:
If you had failed to cover a case, your code wouldn't even compile.
As is, your code compiles, but it doesn't fulfill the requirements of a compare method. Different problem.
Here is the general contract:
- If a<b and b<c, then a<c.
- If a==b, then compare(a, b) must return 0.
- If a<b then b > a.
- you may throw exceptions if you want.
You've broken the contract: If I pass the same value to your compare method, the final case occurs, and -1 is returned, which is saying: 'a is below a', and that breaks the rule.
Expand that last node (the -1) to: (a[1] < b[1]) ? -1 : 0
.
答案2
得分: 0
a[1] 和 b[1] 交换时没有对称性。以下内容可以解决这个问题。
return (a[0] > b[0]) ? 1
: (a[0] < b[0]) ? -1
: (a[1] > b[1]) ? 1
: (a[1] < b[1]) ? -1
: 0;
对于这种结构性比较,应使用 Comparator
类。类似这样:
Arrays.sort(intervals, Comparator.comparing(c -> c[0]).thenComparing(c -> c[1]));
英文:
There is no symmetry when a[1] and b[1] are switched. The following would solve that.
return a[0] > b[0]) ? 1
: a[0] < b[0] ? -1
: a[1] > b[1] ? 1
: a[1] < b[1] ? -1
: 0;
The Comparator
class should be used for such structural comparisons.
Something like:
Arrays.sort(intervals, Comparator.comparing(c -> c[0]).thenComparing(c -> c[1]));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论