我有一个点对象列表,我想按某个坐标排序,例如x值。Java是否提供任何有用的机制,还是我应该利用一种常见的排序算法?
3 回答

开心每一天1111
TA贡献1836条经验 获得超13个赞
在Point课堂上,您应该使用通用类型实现Comparable接口,<Point>并使用Collections.sort(java.util包)进行排序List<Point>
假设:
class Point implements Comparable<Point>{
int compareTo(Point other){ /* your logic */}
}
List<Point> list = new ArrayList<Point>();
/* adding points */
Collections.sort(list);

白衣染霜花
TA贡献1796条经验 获得超10个赞
您应该使自己的点类实现Comparable接口,或者为您自己的Comparator对象提供sort()方法,该对象告诉sort()如何对对象进行排序。这里有很多例子。
添加回答
举报
0/150
提交
取消