2014-03-05 100 views
3
CompanyDAO companyDAO = new CompanyDAO(); 
List companyList = companyDAO.getAllCompanyList(); 

MycompanyList包含了这样一个数据:类型不匹配:不能从对象转换为列表

[[1, Agastha Medical Center], [2, Agastha Asthma]] 

现在我想重复的值,我想传递的值为1查询,但是当我正在把这个内部的for循环我越来越

for(int k=0;k<=companyList.size();k++){ 

List companyId =companyList.get(k); // [1, Agastha Medical Center] for k=0; 

Type mismatch: cannot convert from Object to List 

我需要读取for循环里面的值1我怎么能做到这一点?

+0

不使用普通的'List',使用类型'名单' –

回答

2

当你的companyList是原始类型的,你要投从中获得明确

List companyId = (List) companyList.get(k); 

的对象,但它会更好地提供您的API使用类型,使铸件是没有必要的。

1
  1. 不要使用原始类型使用泛型。例如:Arraylist<String>
  2. List companyId = companyList.get(k)是您的错误。 companyList.get(k)返回一个对象。您必须明确地将其转换为 适当的类型。
1

提供你想从CompanyDAO

防爆拿到类型列表中。 ArrayList<Company>List<Company>

1

你必须键入投列表

for(int k=0;k<=companyList.size();k++){ 

List companyId =(List)companyList.get(k); 
相关问题