简体   繁体   English

Double.compare(a[0],b[0])) 如何工作?

[英]How Double.compare(a[0],b[0])) works?

I want to sort the 2D array我想对二维数组进行排序

int[][] a1 = new int[][2];

and contents are和内容是

6, 8
5, 7
1, 3
2, 4

my expected output is:我预期的 output 是:

1, 3
2, 4
5, 7
6, 8

I found that we can use Double.compare() method as,我发现我们可以使用 Double.compare() 方法,

Arrays.sort(a1,(a,b)->Double.compare(a[0],b[0]));

This did sort the array as I wanted, but I don't know how it is working?这确实按照我的意愿对数组进行了排序,但我不知道它是如何工作的? can someone explain有人可以解释一下吗

Arrays.sort(collection, oracle) is the API you are using here. Arrays.sort(collection, oracle)是您在此处使用的 API。 The collection is a list of (thingies), and the oracle is code (not a value, it's code , or at least, a value pointing at code).该集合是(事物)的列表,而 oracle 是代码(不是值,而是代码,或者至少是指向代码的值)。 The oracle can decide which of 2 things is to be sorted 'before' the other. oracle 可以决定将两件事中的哪一件排在另一件之前。

Given an oracle that can tell you for any 2 (thingies) which one 'comes before', and a collection of (thingies), you can sort it efficiently.给定一个 oracle 可以告诉您任何 2 个(事物)“之前”,以及(事物)的集合,您可以有效地对其进行排序。 How?如何? Well, various algorithms - point is, Arrays.sort is that algorithm and just like you can drive a car without knowing how a combustion engine works, you can use Arrays.sort without knowing those kinds of details.好吧,各种算法——重点是, Arrays.sort就是那个算法,就像你可以在不知道内燃机如何工作的情况下驾驶汽车一样,你可以在不知道这些细节的情况下使用Arrays.sort If you'd like to know - java's libraries are open source, you can have a look.如果你想知道——java 的库是开源的,你可以看看。

So, what's happening here?那么,这里发生了什么?

Your list ( a1 ) is a list of (thingies).您的列表 ( a1 ) 是 (thingies) 的列表。 (thingies) here is int[] . (thingies) 这里是int[] After all, you have an int[][] , and that is not a '2D int array' (that does not exist in java).毕竟,您有一个int[][] ,这不是一个“二维 int 数组”(在 java 中不存在)。 It is an array of int arrays.它是一个 int arrays 数组。 (Which you can trivially use to emulate a 2D int array, so that's nice). (您可以轻松地使用它来模拟 2D int 数组,这很好)。 So, When you try to pass a variable of type int[][] to Arrays.sort , you've handed an array of int[] (well, an array of them).因此,当您尝试将int[][]类型的变量传递给Arrays.sort时,您已经传递了一个int[]数组(嗯,它们的数组)。

Thus, the oracle needs to decide which of 2 int arrays is 'earlier'.因此,oracle 需要决定 2 个 int arrays 中的哪一个是“较早的”。

And it does just that: The (a, b) are the 2 int arrays, and the code then decides which of those 2 is 'earlier' by looking at their first value ( a[0] and b[0] ) and then handing them to the Double.compare method which just.. compares 2 doubles the way you'd think (5 is 'before' 8.2, because, well, it is).它就是这样做的: (a, b)是 2 个 int arrays,然后代码通过查看它们的第一个值( a[0]b[0] )来决定这两个中的哪一个“更早”,然后将它们交给Double.compare方法,该方法只是.. 以您认为的方式比较 2 双倍(5 是“在”8.2 之前,因为它是)。

Why Double ?为什么Double No reason, no idea.没有理由,没有想法。 Integer would make for more efficient and more readable code; Integer将提高代码的效率和可读性; all int can be converted to double without loss, so Double.compare(someInt, someOtherInt) always returns the exact same thing as Integer.compare(someInt, someOtherInt) .所有int都可以转换为double而不会丢失,因此Double.compare(someInt, someOtherInt)总是返回与Integer.compare(someInt, someOtherInt)完全相同的东西。 It's just less readable and less efficient.它的可读性和效率较低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM