可能重複:
why does List<String>.toArray() return Object[] and not String[]? how to work around this?爲什麼List.toArray()返回一個Object數組?
參見該代碼例如:
List<String> list = new ArrayList<String>();
list.add("hello");
list.add("world");
Object [] array = list.toArray();
爲什麼該方法toArray()
返回的Object
數組?其他List
方法如get()
,remove()
能夠返回列表中的對象的類型。
@SuperChafouin請發佈提問之前做一些研究。 –
好的,這回答我的問題:[http://stackoverflow.com/a/7909755/1075341](http://stackoverflow.com/a/7909755/1075341) 我標記我的問題重複。 –
對於那些對這個問題感興趣的人,我發現最好的理解方式是嘗試使用泛型來編寫自己的'toArray'實現,或者查看'ArrayList'代碼。 –