问题
CompanyDAO companyDAO = new CompanyDAO();
List companyList = companyDAO.getAllCompanyList();
MycompanyList contains a data of like this :
[[1, Agastha Medical Center], [2, Agastha Asthma]]
Now i want to iterate the values and i want to pass a value of 1 to query but when i am placing this inside of a for loop i am getting
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
I need to read value of 1 alone inside of for loop how can i do this ?
回答1:
As your companyList is of raw type, you have to cast the object obtained from it explicitly
List companyId = (List) companyList.get(k);
But it would be better to provide your API with types so that casting is not necessary.
回答2:
you have to type cast the list
for(int k=0;k<=companyList.size();k++){
List companyId =(List)companyList.get(k);
回答3:
- Don't use raw types use generics. example :
Arraylist<String>
- List companyId =companyList.get(k) is your error. companyList.get(k) returns an object. you have to typecast it to appropriate type explicitly.
回答4:
Provide the Type of list you want to get from CompanyDAO
Ex. ArrayList<Company>
, List<Company>
来源:https://stackoverflow.com/questions/22194413/type-mismatch-cannot-convert-from-object-to-list