说,我们有以下二维数组:int camels[][] = new int[n][2];应该如何Comparator声明Java 类,以便使用降序按数组的第一个元素对数组进行排序Arrays.sort(camels, comparator)?compare供参考的功能是:@Override public int compare(int[] a, int [] b){ return b[0] - a[0];}
2 回答

慕娘9325324
TA贡献1783条经验 获得超4个赞
我只想为Java 8添加另一种方法。
int[][] twoDim = { { 1, 2 }, { 3, 7 }, { 8, 9 }, { 4, 2 }, { 5, 3 } };
Arrays.sort(twoDim, (int[] o1, int[] o2) -> o2[0] - o1[0]);
System.out.println(Arrays.deepToString(twoDim));
对我而言,使用Java 8语法非常直观且易于记忆。
添加回答
举报
0/150
提交
取消