【发布时间】:2015-07-08 20:51:06
【问题描述】:
我有一个具有双值的 ArrayList:
double distance = HaversineAlgorithm.HaversineInKM(lat, lon,
stop.getStopLat(), stop.getStopLon());
distanceList.add(distance);
我可以用这个得到列表中的最小值:
int minIndex = distanceList.indexOf(Collections.min(distanceList));
currentList.get(minIndex); //it is the smallest value element
但我也必须知道第二个最小值和第三个最小值元素。但我不知道怎么做。
我试图用这个对列表进行排序:
Collections.sort(distanceList);
但它不起作用。我的意思是 List 的最后一个索引不是最小值或最大值。
这里有人可以解释我如何解决这个问题吗? :)
【问题讨论】:
-
排序后,min 元素位于索引 0(假设排序是自然顺序),因此您希望元素位于索引 1。也许您应该澄清“但它不起作用。”
-
这将按升序排列,所以,
sort()之后,distanceList.get(0)是最小的,distanceList.get(1)是第二小的,依此类推... -
currentList.get(minIndex + 1);为第二大,currentList.get(minIndex + 2);为第三大