2011-09-13 43 views
2

通常,代碼塊完美無缺。但在非常罕見的情況下,「新ArrayList」會拋出一個Exeption my.namespace.CacheEntry不能存儲在類型爲java.lang.Object []的數組中。創建ArrayList:(某個對象)不能存儲在java.lang.Object類型的數組中[]

檢查谷歌,別人似乎得到這個例外與宏基A500與3.1(這也是我得到它的設備)。在泛型Java或其他類似的東西中,我沒有看到任何這樣的命中,所以可能會有一些非常非常非常蜂巢式的特殊情況,甚至是虛擬機bug。

private long expireCache(HashMap<String, CacheEntry> map) { 
    long count = 0; 
    // next line will sometimes throw the exception: 
    ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>(map.values()); 
    Collections.sort(entries); 

的CacheEntry類是相當有規律,太:

final class CacheEntry implements Comparable<CacheEntry> { 
    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); 
     } 
    } 

我看不出什麼毛病此代碼。任何人?

+0

你在哪裏以及如何調用expireCache?請舉例。 – Alexandr

+0

你的代碼似乎是正確的,它確實看起來像一個蜂窩錯誤。 – Guillaume

+0

@Alexandr,我認爲這個問題在什麼時候以及如何調用這個函數完全沒有關係。但是如果你真的需要知道,它會從syncronized(cacheentry-hashmap)條目中調用(因爲對hashmap的所有更改都在同步內),當緩存的大小或映射中的元素數量超過高水位。 – Oliver

回答

1

可能是一些非常非常非常蜂巢特殊情況,甚至是虛擬機的bug?

是的,看起來像它,因爲根據Java語義,有任何「不能被存儲在類型的java.lang.Object中[]數組」 - 除了原語,但這些可以是不是值在HashMap

相關問題