英文:
Arrays.sort(ind, (l, r) -> nums[l] - nums[r]);
问题
这段代码是使用Java编写的,主要涉及了数组排序和Lambda表达式的使用。该代码的目的是根据给定的 nums 数组中元素的值对 ind 数组进行排序。
首先,代码使用 Arrays.sort 方法对 ind 数组进行排序,排序的规则由Lambda表达式指定。Lambda表达式 (l, r) -> nums[l] - nums[r] 定义了排序的方式,它会比较 l 和 r 两个元素在 nums 数组中对应位置的值,然后根据差值进行排序。如果差值为正,就交换它们的位置,如果差值为负或零,就保持它们的位置不变。
接下来,给定了 nums 数组和初始的 ind 数组。然后,代码通过不断调用Lambda表达式中的比较操作来排序 ind 数组。
具体的步骤如下:
- 初始状态下,
l = 0,r = 1,因为ind数组中的第一个元素是0,第二个元素是1。 - 根据Lambda表达式比较
nums[0] - nums[1],结果为正数,所以交换ind数组中的元素位置,变为[1, 0, 2]。 - 接下来,
l = 1,r = 2,因为ind数组中的第一个元素已经是1,所以取下一个元素2。 - 再次根据Lambda表达式比较
nums[1] - nums[2],结果为负数,所以不交换元素位置,保持为[1, 0, 2]。 - 最后,
l = 0,r = 2,因为ind数组中的第一个元素已经是1,所以取下一个元素0。 - 再次根据Lambda表达式比较
nums[0] - nums[2],结果为正数,所以交换ind数组中的元素位置,变为[1, 0, 2]。
最终得到排序后的 ind 数组为 [1, 0, 2]。
这段代码的关键在于Lambda表达式定义的比较方式,它根据 nums 数组中的值来排序 ind 数组,使得 ind 数组按照 nums 中的对应值从小到大排序。希望这个解释对你有帮助。
英文:
Can anybody explain to me how this piece of code works.
Arrays.sort(ind, (l, r) -> nums[l] - nums[r]);
nums = [3,2,4]
ind = [0,1,2]
I debugged it and noticed that
l = 1
r = 0
then
l = 2
r = 1
then
l = 2
r = 0
and i get
ind = [1,0,2]
I am confused. Can anyone kindly explain how it worked.
答案1
得分: 1
ind是一个索引值的数组,最初的索引值从0到len-1。
调用sort()将按照在给定索引处的num值对索引值进行排序。
结果是,如果你按照排序后的顺序打印ind中的值对应的num[i],这些值将会是有序的(升序)。
for (int j = 0; j < ind.length; j++) {
int i = ind[j];
int num = nums[i];
System.out.println(num);
}
// 简化版本
for (int i : ind)
System.out.println(nums[i]);
输出
2
3
4
英文:
ind is an array of index values, initially index values from 0 to len-1.
The call to sort() will sort the index values by the value in num at the given index.
The result is that if you print num[i] for the values in ind, in the order present after the sort, the values will be in sorted order (ascending).
for (int j = 0; j < ind.length; j++) {
int i = ind[j];
int num = nums[i];
System.out.println(num);
}
// Short version
for (int i : ind)
System.out.println(nums[i]);
Output
2
3
4
答案2
得分: 0
这段代码使用了Arrays.sort(T[] a, Comparator<? super T> c)。
这个函数接受两个参数:
- 要排序的数组
Array - 一个
Comparator
Comparator 是一个只有一个方法的接口:public int compare(Object obj1, Object obj2)。这个方法返回 x < 0、0 或 x > 0,分别表示 obj1 分别是 小于、等于 或 大于 obj2。
所以,由于 Comparator 只有一个方法,你可以使用 lambda 表达式,就像这样:(l, r) -> nums[l] - nums[r]。
这是“我正在创建一个新的比较器,以下是其唯一方法的操作”的简化版本:
(l, r):它将 l 和 r 作为参数nums[l] - nums[r]:它返回该表达式的结果。
正如你可以看到的:
- 如果
nums[l]大于nums[r],结果是正数 - 如果
nums[l]等于nums[r],结果是0 - 如果
nums[l]小于nums[r],结果是负数
英文:
This code is using Arrays.sort(T[] a, Comparator<? super T> c).
This function is taking in parameter :
- The
Arrayyou want to sort - A
Comparator
Comparator is a interface that has just one method : public int compare(Object obj1, Object obj2). This method returns x < 0, 0 or x > 0 if obj1 is respectively lower than, equal or greater than obj2.
So, as Comparator has a unique method you can use a lambda, that's what is done here : (l, r) -> nums[l] - nums[r].
This is a short version of "I am creating a new Comparator and here is what its unique method will do" :
(l, r): it takes l and r as argumentsnums[l] - nums[r]it returns the result of that expression.
As you can see :
- if
nums[l]>nums[r]the result is positive - if
nums[l]=nums[r]the result is 0 - if
nums[l]<nums[r]the result is negative
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论