gpt4 book ai didi

java - 对整数数组的 ArrayList 进行排序

转载 作者:行者123 更新时间:2023-11-29 10:15:16 25 4
gpt4 key购买 nike

如何根据整数数组中的最后一个整数对整数数组的 ArrayList 进行排序?

ArrayList<int[]> paths = new ArrayList<int[]>();
paths.add(new int[]{0,0,0,0,4});
paths.add(new int[]{0,0,0,0,2});
paths.add(new int[]{0,0,0,0,1});
paths.add(new int[]{0,0,0,0,3});

生成的 ArrayList 将包含:[0,0,0,1] [0,0,0,2] [0,0,0,3] [0,0,0,4]

最佳答案

实现一个比较器并使用Collections.sort。或者同时进行:

Collections.sort(paths, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
return (Integer)(a[a.length-1]).compareTo(b[b.length-1]);
}
});

关于java - 对整数数组的 ArrayList 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19596950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com