我正與通用和數組玩,看來下面的代碼編譯好,數組泛型列表
ArrayList<Key> a = new ArrayList<Key>();
但是,編譯器會抱怨這個,
ArrayList<Key>[] a = new ArrayList<Key>[10];
通過計算器讀後,我有點理解,這是由於類型擦除,我可以修復它通過使用,
ArrayList<Key>[] a = (ArrayList<Key> []) new ArrayList[10];
或列表列表
ArrayList<ArrayList<Key>> b = new ArrayList<ArrayList<Key>>();
但我無法弄清楚幕後的原因。特別是,爲什麼第二個是非法的,因爲第一個是非常好的。爲什麼編譯器不會抱怨列表的列表。
http://stackoverflow.com/questions/217065/cannot-create-an-array-of-linkedlists-in-java – tcb