【问题标题】:What does compareTo method return in javajava中的compareTo方法返回什么
【发布时间】:2017-05-07 13:39:53
【问题描述】:

我有一个代码 sn-p,它在数组中找到中位数,然后根据与中位数的接近程度对该数组进行排序(从最小到最大),但我不明白这些东西是如何工作的

 public static Integer[] sort(Integer[] array)
    {
        Arrays.sort(array);
        final double median;
        if (array.length % 2 == 0)
            median = ((double)array[array.length/2-1]+ (double)array[array.length/2])/2;
        else
            median =  array [array.length/2];

        Comparator<Integer> compareToMedian= new Comparator<Integer>()
        {
            @Override
            public int compare(Integer o1, Integer o2)
            {
                double value = Math.abs(o1 - median) - Math.abs(o2 - median);
                if (value == 0)
                    value = o1 - o2;
                return (int)value;
            }
        };

        Arrays.sort(array, compareToMedian);
        return array;
    }

我真正感到困惑的是在这种特殊情况下价值的确切含义。它只是从最小到最大排序结果吗?就像我有result 在一种情况下等于-5,在另一种情况下等于-2 和3,最终它变成-5、-2、3?并对数组进行排序?

【问题讨论】:

  • 您是否阅读了Comparator.compare 的文档?您在调试此代码方面尝试过什么?目前,您的“像我一样”这句话对我来说没有多大意义,尤其是 result 不会出现在代码中的任何地方......
  • :(:(:(:(:(:(:(:(
  • 您是在问compare 的返回值是什么意思,还是在问Math.abs(o1 - median) - Math.abs(o2 - median) 的数学运算原理?另请参阅docs.oracle.com/javase/tutorial/collections/interfaces/…
  • 我在问排序基于什么。返回值还是什么?如果是这样,它是如何工作的?
  • 我想你的意思是compare,而不是compareToComparatorcompare,这就是您的代码中的内容。 ComparablecompareTo

标签: java comparator compareto


【解决方案1】:

compareTo() 返回一个 int 以显示哪个值更大

如果两者相等则为0

+ve 如果第一个值更大

-ve 如果第二个值更大


compareTo(5,5) 返回 0

compareTo(6,5) 返回正数

compareTo(5,6) 返回任何负数

//    going line by line for explanation

    public static Integer[] sort(Integer[] array)
    {
        Arrays.sort(array);//first of all sort the numbers in increasing order
        final double median;
        /*
        now in sorted numbers median is the
        --- average of middle two values for even count of numbers; like for 10 numbers median is (4th item +5th item )/2
        --- middle value if there are odd count of numbers like for 11 items the median is the 6th item
         */
        if (array.length % 2 == 0)
            median = ((double)array[array.length/2-1]+ (double)array[array.length/2])/2;
        else
            median =  array [array.length/2];
        //now we have the median



//        here we have a Comparator for comparing any value during with the median value
        Comparator<Integer> compareToMedian= new Comparator<Integer>()
        {
            @Override
            public int compare(Integer o1, Integer o2)
            {
//                first we check the distance of two numbers from the median; that is the difference from the median
                double value = Math.abs(o1 - median) - Math.abs(o2 - median);
                if (value == 0)//if the difference is same then we compare the numbers
                    value = o1 - o2;
                return (int)value;//otherwise we return the difference
            }
        };

        Arrays.sort(array, compareToMedian);//sort the numbers with respect to median
        return array;
    }

【讨论】:

  • 非常感谢,这有点帮助,但我仍然想知道返回值如何影响数组顺序。它是否只是使用冒泡排序或什么检查数组中的每个值以及返回值?
  • 您使用 Compartor 给出的规则(在 compareTo 方法中)比较数组中的两个项目,直到最后。进一步的 Arrays.sort 不是冒泡排序,而是更快的排序技术。
  • @javafan 谢谢,不错的网站顺便说一句 Quadgen。希望我把代码说清楚了。