Type mismatch: cannot convert from Object to List

我的未来我决定 提交于 2019-12-14 03:52:52

问题


 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:


  1. Don't use raw types use generics. example : Arraylist<String>
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!