我有以下類。在這方面,虹膜是另一類有一些屬性的類。在Java中排序數組列表
public class Helper {
Iris iris;
double distance;
public Helper(Iris iris, double distance) {
this.iris = iris;
this.distance = distance;
}
}
我要排序的這樣的數組列表(即列表<助手> helperList),基於距離參數遞減。我寫了下面的方法,但它不起作用。
public void sort(){
for(int k=0; k < helperList.size(); k++)
{
double distance = helperList.get(k).distance;
for(int l=0; l < helperList.size(); l++)
{
Helper temp = helperList.get(l);
if(distance < temp.distance)
{
helperList.set(l, helperList.get(k));
helperList.set(k, temp);
}
}
}
}
有人可以提出解決方案嗎?
以何種方式是不工作? – 2012-01-01 07:49:40
它沒有正確排序列表。 – Ammar 2012-01-01 07:53:13
考慮兩個元素會發生什麼,比如'[1,2]'。對於'k = 0,l = 1',因爲'list.get(1)> 1',你交換,給出'[2,1]'。然後,對於'k = 1,l = 0',您再次交換。您應該將每個元素僅與其一側的元素進行比較。 – 2012-01-01 08:00:45