我轉換列表時,列出如何轉換列表<?>列出<className>
List<ContentDes> contentDes_ls = new ArrayList<ContentDes>();
logger.info("in getContentDes ");
List<?> ls = ho.getResultListByLimit(sql,limit);
contentDes_ls = (List<ContentDes>)ls;
logger.info(" size of content "+contentDes_ls.size());
for (ContentDes contentDes : contentDes_ls) {
logger.info(contentDes.getPricetag());
logger.info(contentDes.getPrv());
}
它工作得很好,當我得到的List<SomeClass>
大小,但面臨着一些問題,當我 訪問SomeClass
我的getter和setter有例外
輸出:未提及
size of content 2
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.SomeClass]
通用類型不能直接進行類別轉換。您可以使用Object迭代列表>,並通過instanceof判斷類,並進行類投射。 – MageXellos