代码:
public List<InvoicePostAddressForm> invoicePostAddressListByStoreId(String storeId) throws BusinessException {
List voList = new ArrayList();
if (!result.getSuccess()) {
throw new BusinessException(result.getCode());
}
voList = (List)result.getResult();
ArrayList<InvoicePostAddressForm> formList = new ArrayList<InvoicePostAddressForm>();
for (InvoicePostAddressVo vo : voList) { // here's error
formList.add(this.ipavo2Form(vo));
}
return formList;
}
代码来自反编译项目,我没有源代码,并且
for (InvoicePostAddressVo vo : voList)
在编辑器中显示错误,我使用Idea
Incompatible types.
Required:Object
Found:InvoicePostAddressVo
InvoicePostAddressVo是一个对象,它需要一个对象,为什么显示此错误?
答案 0 :(得分:4)
此行的原因:
List voList = new ArrayList();
您是否打算这样做,而不是像这样?
List<InvoicePostAddressVo> voList = new ArrayList<>();
当您尝试用行来遍历集合
for (InvoicePostAddressVo vo : voList)
编译器期望voList保证所有元素都属于InvoicePostAddressVO类型,这与Object列表不同。
强烈建议您在没有正当理由的情况下使用原始列表。