Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 排序时,比较法违反其总合同_Java_Sorting_Comparison - Fatal编程技术网

Java 排序时,比较法违反其总合同

Java 排序时,比较法违反其总合同,java,sorting,comparison,Java,Sorting,Comparison,我不断得到:比较法违反了它的总合同调用Arrays.sort(ScreenItems)时下面的比较函数出现异常 我的一个假设是,下面的ParseInt为左对象而不是右对象抛出异常 会是这样吗 public int compare(Object o1, Object o2) { if (o2 == null || o1 == null) return 0; if (!(o1 instanceof ScreenItem) || !(o2 instanceof Scr

我不断得到:比较法违反了它的总合同调用Arrays.sort(ScreenItems)时下面的比较函数出现异常
我的一个假设是,下面的ParseInt为左对象而不是右对象抛出异常
会是这样吗

public int compare(Object o1, Object o2) {
    if (o2 == null || o1 == null)
        return 0;

    if (!(o1 instanceof ScreenItem) || !(o2 instanceof ScreenItem))
        return 0;

    ScreenItem item1 = (ScreenItem) o1;
    ScreenItem item2 = (ScreenItem) o2;

    String subSystem1 = item1.getSubSystem();
    String subSystem2 = item2.getSubSystem();

    if(subSystem1.equals(subSystem2)) {
        return 0;
    } else if(subSystem1.startsWith(subSystem2)) {
        return 1;
    } else if (subSystem2.startsWith(subSystem1)) {
        return -1;
    }

    String order1 = item1.getOrder();
    String order2 = item2.getOrder();

    if (order1 == null || order2 == null){
        String name1 = item1.getName();
        String name2 = item2.getName();

        if(name1 == null || name2 == null)
            return 0;

        return name1.compareToIgnoreCase(name2);
    }

    try {
        return Integer.parseInt(order1) - Integer.parseInt(order2);
    } catch (Exception ex) {
        return 0;
    }
}

这是一个我认为需要进行的变革的例子。正如@CommuSoft在一篇评论中指出的那样,当前对
o1
o2
null
处理破坏了及物性

我将替换:

if (o2 == null || o1 == null)
    return 0;
与:

这将
null
视为与自身相等,但小于所有非null引用。当然,您也可以选择将
null
视为大于所有非null引用,只要您是一致的。如果有任何两个非null引用返回一个非零值,那么像当前代码中那样将其视为等于所有内容是不一致的


更一般地说,我建议为排序编写一组规则,确保它们符合
比较器
契约,然后编写代码和测试以匹配这些规则。

我建议将日志代码放在catch块中,而不是假设异常。在任何情况下,如果您计划在异常发生后继续计算,您应该更具体地了解捕获的内容。要详细说明@PatriciaShanahan的评论,您可能应该检查两个操作数中的哪一个在解析时抛出异常,并为以下内容制定单独的规则:(1)无效操作有效,(2)有效操作无效,以及(3)无效op无效;所有这些都应该沿着为非数字顺序字符串指定固定排序位置的路线(例如,非数字顺序在
-INT_MAX
之前)。我认为应该使用
o2==null | | o1==null
@CommuSoft:Huh?这是这个方法的第一件事。(但我并不确定我认为这是对的。我希望实例不等于
null
)@T.J.Crowder:问题是传递性不再得到保证。因为
null==a
意味着
null
,这违反了订单关系的传递性。正如所说的,您应该将
null
处理为无穷远。
if (o2 == null && o1 == null)
    return 0;
if (o1 == null)
    return -1;
if (o2 == null)
    return 1;