java.lang.ClassCastException: java.util.ArrayList 不能转换为 antlr.collections.List

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35192608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:24:59  来源:igfitidea点击:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to antlr.collections.List

javalistarraylist

提问by Khushbu Soni

If I do not do type casting to query.list(); to (List)query.list(); it gives me these errors.

如果我不将类型转换为 query.list(); 到(列表)query.list();它给了我这些错误。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

java.lang.ClassCastException: java.util.ArrayList cannot be cast to antlr.collections.List

java.lang.ClassCastException: java.util.ArrayList 不能转换为 antlr.collections.List

on calling a method

在调用方法时

category= new Category();
ArrayList data= (ArrayList) category.getCategoryInfo();

and method is

方法是

public List  getCategoryInfo(){
      Session session = HibernateUtil.openSession();

      String sql = "SELECT category.id as id, category.name,c.name as parent_category FROM category join category as c on c.id=category.parent_id"; 
      SQLQuery query = session.createSQLQuery(sql);
      query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); 
      List data = (List) query.list();
      return data;
}

Please help me what is wrong with this code.

请帮我看看这段代码有什么问题。

采纳答案by Sabir Khan

You need to change line, List data = (List) query.list();to List data = query.list();as query.list();always returns a Listso no need to explicit casting.

你需要改变路线, List data = (List) query.list();List data = query.list();作为query.list();总是返回List所以无需显式类型转换。

Also , at assignment part, you again need to program on Interface Listbut you are doing on concrete class - ArrayList.

此外,在作业部分,您再次需要编程,Interface List但您正在具体类 - ArrayList.

ArrayList data= (ArrayList) category.getCategoryInfo();needs to be changed to List data= category.getCategoryInfo();

ArrayList data= (ArrayList) category.getCategoryInfo();需要改为 List data= category.getCategoryInfo();

When you program on super type interfaces, need to explicit casting is eliminated.

当您在超类型接口上编程时,不需要显式转换。

Note that Listinterface returned from method getCategoryInfo()and one written at assignment part should be the same interfaces ( either java.util.ListOR antlr.collections.List, you can't mix the two ).

需要注意的是List,从方法返回的接口getCategoryInfo(),一个在分配部分写应该是相同的接口(无论是java.util.Listantlr.collections.List,你不能混用两种)。

Figure out on your own about the kind of Listreturned by query.list();

自己弄清楚List返回的类型query.list();

回答by Diego Farias

It seems like you've imported the wrong List class. Look at the imports of your class and replace antlr.collections.List for java.util.List.

您似乎导入了错误的 List 类。查看您的类的导入并将 antlr.collections.List 替换为 java.util.List。