我有一个类,里面有很多字段,如下所示:Unit
public class Unit {
private final int id;
private final int beds;
private final String city;
private final double lat;
private final double lon;
// constructors and getters here
// toString method
}
我现在有一个列表,其中是一个包含许多单位的对象。现在我需要找到从对象到 最近的单位。通过限制限制结果。UnitListListUnit x
private List<Unit> nearestUnits(List<Unit> lists, Unit x, int limit) {
List<Unit> output = new ArrayList<>();
// how do I sort lists object in such a way so that I can get nearest units here to "x"?
return output;
}
我们在类中存在纬度/经度,因此我们可以使用它来计算欧氏距离并进行比较。我对如何按最短距离对单位列表进行排序并获取最近的单位感到困惑。到目前为止,我正在使用 Java 7,所以我不能使用 Java 8。Unit
临摹微笑
PIPIONE
相关分类