通常情况下,代码块是完美的。但在非常罕见的情况下,“新ArrayList”会抛出一个Exeption my.namespace.CacheEntry不能存储在类型为java.lang.Object []的数组中。
检查谷歌,别人似乎得到这个例外与宏基A500 3.1(这也是我得到的设备)。在泛型Java或其他类似的东西中,我没有看到任何这样的命中,所以可能会有一些非常非常非常蜂巢式的特殊情况,甚至是虚拟机bug。
private long expireCache(HashMap map) {
long count = 0;
// next line will sometimes throw the exception:
ArrayList entries = new ArrayList(map.values());
Collections.sort(entries);CacheEntry类也很常见:
final class CacheEntry implements Comparable {
public File file;
public Long time;
CacheEntry(File cacheFile) {
// retreive the lastModified only once, don't do heavy I/O for sorting,
// keep it desynced from filesystem so nothing bad happens when the
// file gets changed while sorting:
file = cacheFile;
time = cacheFile.lastModified();
}
// "touch" the cached last modified time
public void touch() {
time = System.currentTimeMillis();
}
// return the long comparable of last modified time
public int compareTo(CacheEntry c) {
return time.compareTo(c.time);
}
}我没有看到这个代码有什么问题。任何人?