Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按时间搜索列表中的元素数_Java_Android - Fatal编程技术网

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

Java 按时间搜索列表中的元素数,java,android,Java,Android,我有一张物品清单。对象有字段:名称、姓氏和时间(长)。我想检查此列表中有多少项具有相同的时间。我将long转换为HH:mm,现在我需要知道,举例来说,有多少项的时间为0,有多少项的时间为1,等等。类似如下: hours - number of elements 0 - 10 1 - 2 2 - 4 ... 23 - 13 etc until get all day hours. 有什么想法吗?我怎样才能实现这个想法 ArrayList<Person>

我有一张物品清单。对象有字段:名称、姓氏和时间(长)。我想检查此列表中有多少项具有相同的时间。我将long转换为HH:mm,现在我需要知道,举例来说,有多少项的时间为0,有多少项的时间为1,等等。类似如下:

hours - number of elements
0     - 10
1     - 2
2     - 4
...
23    - 13
etc until get all day hours.
有什么想法吗?我怎样才能实现这个想法

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

for(Person p : people) {
    count[hourOf(p.time)]++;
}
其中,
hhmm.indexOf(“:”)
可以替换为
2
,前提是所有的小时数都是
0
填充的

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