当遇到类似问题时,深入研究问题并找到违反一般契约的 A、b 和 c 集合的唯一方法是使用循环。
假设您有一个需要排序的list 和一个违反其合同的自定义comparator,您可以使用以下内容找到对象
for (int i = 0; i < list.size(); i ++) {
for (int j = 0; j < list.size(); j ++) {
for (int k = 0; k < list.size(); k ++) {
Objects a = list.get(i);
Objects b = list.get(j);
Objects c = list.get(k);
if (comparator.compare(a, b) < 0
&& comparator.compare(b, c) < 0
&& comparator.compare(a, c) > 0) {
System.out.print(("Error...1");
System.out.print((a + ", " + i);
System.out.print((b + ", " + j);
System.out.print((c + ", " + k);
}
if (comparator.compare(a, b) > 0
&& comparator.compare(b, c) > 0
&& comparator.compare(a, c) < 0) {
System.out.print(("Error...2");
System.out.print((a + ", " + i);
System.out.print((b + ", " + j);
System.out.print((c + ", " + k);
}
if (comparator.compare(a, b) == 0
&& comparator.compare(b, c) == 0
&& comparator.compare(a, c) != 0) {
System.out.print(("Error...3");
System.out.print((a + ", " + i);
System.out.print((b + ", " + j);
System.out.print((c + ", " + k);
}
}
}
}
这种方法我以前用过很多次,尤其是当你无法通过检查发现你的编码中的逻辑错误时。
我还在另一个帖子中找到了这个答案,该帖子有一个你可以使用的通用类
https://stackoverflow.com/a/35000727/4019094