我有網格將呈現一個日曆,並提供了一個ArrayList<CalendarEventEntity>
其中包含事件。這些事件必須在網格中突出顯示。如何改進填充日曆網格的算法?
正如我自己一個人填補網格我有這樣的事情:
for(loop through the days of the month){
Calendar eventDate = event.getDate();
// look for the events in the calendar that matchs this day
for(CalendarEventEntity event : events) {
// if there are events in this specific day
if(eventDate.get(Calendar.YEAR) == calendarMonth.get(Calendar.YEAR) &&
eventDate.get(Calendar.MONTH) == calendarMonth.get(Calendar.MONTH) &&
eventDate.get(Calendar.DAY_OF_MONTH) == dayIndex) {
// highlight it!!!
}
}
}
這工作得很好,但它的速度太慢。所以我想加快它!我內for
前加入這樣的:
// ignore dates which does not make part of this month or year
if(eventDate.get(Calendar.YEAR) < calendarMonth.get(Calendar.YEAR) ||
eventDate.get(Calendar.MONTH) < calendarMonth.get(Calendar.MONTH) ||
eventDate.get(Calendar.DAY_OF_MONTH) != DateIdx) {
continue;
}
// stop when processing dates which are higher than this month or year
if(eventDate.get(Calendar.YEAR) > calendarMonth.get(Calendar.YEAR) ||
eventDate.get(Calendar.MONTH) > calendarMonth.get(Calendar.MONTH)
|| eventDate.get(Calendar.DAY_OF_MONTH) != DateIdx) {
break;
}
而且如果做快,但它仍然太慢。我該如何改進這個算法?
我還沒有想過一個HashMap。我現在就試試看看是否有幫助。雖然,我不想讓它太複雜,並且使用HashMap將必須如下所示:'HashMap>'。 –
Cristian
2010-08-17 14:10:32
我自己多次使用過這種模式。如果您使用的是多線程應用程序,請確保同步對HashMap的所有訪問以避免衝突。 – 2010-08-17 16:21:49