type casting - generics
遇到一個奇怪的問題,
開發環境是eclipse with JDK1.5
簡單的code 如下:
final String FILE = "filename";
ArrayList<String> list = new ArrayList<String>();
....
ObjectInputStream ois =
new ObjectInputStream(new FileInputStream(FILE));
list = (ArrayList<String>) ois.readObject();
ois.close();
問題出在readObject()這部分,訊息是
"Type safety: The cast from Object to ArrayList<String> is
actually checking against the erased type ArrayList"
我也看了這玩意,
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
然後我看了Sun's的ArrayList<T> ,他們的寫法是Object[] -> T[],
除了 @SuppressWarnings 以外,有沒有比較漂亮的寫法呢?
---
http://cscott.net/Projects/GJ/ ....
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 66.108.81.124
java 近期熱門文章
PTT數位生活區 即時熱門文章