Java 按双值对ArrayList排序
我有一个具有双值的ArrayList:Java 按双值对ArrayList排序,java,sorting,arraylist,Java,Sorting,Arraylist,我有一个具有双值的ArrayList: double distance = HaversineAlgorithm.HaversineInKM(lat, lon, stop.getStopLat(), stop.getStopLon()); distanceList.add(distance); 我可以通过以下方法获得列表中的最小值: int minIndex = distanceList.indexOf(Collections.min(distanceL
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);
但我必须知道第二小值和第三小值元素。但我不知道怎么做
我尝试用以下内容对列表进行排序:
int minIndex = distanceList.indexOf(Collections.min(distanceList));
currentList.get(minIndex); //it is the smallest value element
Collections.sort(distanceList);
但它不起作用。我的意思是列表的最后一个索引不是最小或最高的值
有人能解释我如何解决这个问题吗?:) 这段代码看起来不错:
int minIndex = distanceList.indexOf(Collections.min(distanceList));
currentList.get(minIndex); //it is the smallest value element
因此,由于所有内容都是按顺序排序的,因此可以通过直接调用索引来获得最小值、第二小值和第三个smallert值
currentList.get(0);
currentList.get(1);
currentList.get(2);
排序后,min元素位于索引0处(假设排序按自然顺序),所以您希望元素位于索引1处。也许你应该澄清“但它不起作用。”这将按升序排序,因此,在
sort()
之后,distanceList.get(0)
是最小的,distanceList.get(1)
是第二小的,依此类推……currentList.get(minIndex+1)代码>用于第二大和currentList.get(minIndex+2)代码>第三大谢谢,对不起,我的问题不够清楚..:(我有两个ArrayList:currentList,其中包括实体对象..使用HaversineAlgorithm,我正在检查两个gps坐标之间的距离..我将值放入另一个ArrayList这是距离列表..我知道距离列表中哪一个是最小的双值,但我不知道如何指向另一个ArrayList,其中包括我的entity-s:)也许我应该使用HashMap。HashMap
听起来比使用两个数组更好。试着看看它是否有效,如果它没有发布新的问题,我们将帮助你