2015-10-19 30 views
0

我有對象列表。對象包含字段:名稱,姓氏和時間(全部)。我想檢查一下這個列表中有多少項目具有相同的小時。我轉換長HH:MM,現在我需要知道exampole有多少條目0時,有多少時間有時間1等事情是這樣的:按時間搜索列表中的元素數量

hours - number of elements 
0  - 10 
1  - 2 
2  - 4 
... 
23 - 13 
etc until get all day hours. 

任何想法,我怎麼可以簡單地實現這個想法

回答

0
ArrayList<Person> people; 
int[] count = new int[24]; 

for(Person p : people) { 
    count[hourOf(p.time)]++; 
} 

不知道你所儲存的時間如何(即長代表什麼),但如果你有HH:mmString,那麼你可以很容易地計算小時:

int hourOf(String hhmm) { 
    return Integer.parseInt(hhmm.substring(0, hhmm.indexOf(':'))); 
} 

其中hhmm.indexOf(':')可以替換爲2,如果所有小時數均爲0填充。