按时间搜索列表中的元素数

时间:2015-10-19 20:36:58

标签: java android

我有对象列表。对象有字段:名称,姓氏和时间(长)。我想检查此列表中有多少项具有相同的小时数。我将long转换为HH:mm,现在我需要知道exampole有多少项有时间0,有多少时间有时间等等。这样的事情:

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

任何想法我怎样才能简单地实现这个想法

1 个答案:

答案 0 :(得分: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