2013-04-29 24 views
1

我正在爲Android編寫代碼,以限制我從FAST檢測器獲得的關鍵點數量(現在我獲得了大約9000個關鍵點)。我想根據響應保持最佳的500個關鍵點。我製作了一個比較器,可以根據他們的響應對這些關鍵點進行排序。現在我想找到一種方法來獲得500個最佳關鍵點,並將它們放入一個新列表中。如何保持Java/Android中Arraylist的最佳500個關鍵點?

這裏是我的代碼有

// gets the keypoints from the detector, and puts them in a list 
List<KeyPoint> pointstest = points1.toList(); 
       // comparator orders the keypoints (check image for output) 
       order(pointstest); 
        // make a new list to put the 500 best keypoints in 
       List<KeyPoint> nieuw = new ArrayList<KeyPoint>(); 

所以我現在需要「重建」的最佳點的名單,但我目前停留在如何解決這一問題。有沒有人有建議?我正在考慮一個for循環,但是它可以用於這些關鍵點嗎?

回答

0

如何

List<KeyPoint> nieuw = new ArrayList<KeyPoint>(pointstext.subList(0, 500)); 
+0

謝謝,我不知道這是這麼簡單:D – user1393500 2013-04-29 21:04:02

0

使用類別進行排序,以便您可以選擇以500

List<Object> sortedArray = new ArrayList<Object>(count); 
    Collections.sort(sortedArray, new Comparator<Object>() { 
      public int compare(Object o1, Object o2) { 
        //TODO add code for deciding values to compare on 
      return value; 
      } 
    }); 
+0

這不是真的我的問題,我已經在我的代碼中實現了該部分,因此命令(pointstest);但無論如何謝謝你的答案! – user1393500 2013-04-29 21:06:06

2

其實列表中想要的值,你應該結合@Ashwini Bhangi和@Peter Lawrey意見建議:先把你的清單sort,然後得到一個從0到499的子清單。

比較儀正式Comparator<T>,在你的情況下Comparator<KeyPoint>,所以:

int count = 500; 
Collections.sort(keypoints, new Comparator<KeyPoint>() { 
    public int compare(KeyPoint o1, KeyPoint o2) { 
     //TODO add code for deciding values to compare on 
     // Note that here you should implement DESCENDING logic 
     // to get greater values at the beginning, not at the end 
     return value; 
    } 
}); 

然後得到的子表:

List<KeyPoint> theBest = new ArrayList<KeyPoint>(keypoints.subList(0, count)); 

請記住,排序最初的名單修改到位。如果你想保留原始列表,你應該在排序前複製一份。

相關問題