当前位置: 首页 > 知识库问答 >
问题:

java.util.ArrayList不能强制转换为[Ljava.lang.Object[duplicate]

鲜于德泽
2023-03-14
##########  Error with java.lang.ClassCastException: java.util.ArrayList cannot be cast to [Ljava.lang.Object;  ##########
Size 1
 try {
        File file = new File(filePath + "ABC.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        String st;
        List list = new ArrayList();

        try {
            while ((st = br.readLine()) != null) {
                String id = st.substring(92, 100);
                try {
                    list.add(getDetail(id));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                for (Object[] o : (List<Object[]>) list) {
                    Details x = new Details();
                    x.setType(o[0].toString());
                     ....
                }
            }

            br.close();

        } catch (Exception ex) {
            logger.printError(Reader.class, "Error with " + ex);
        }
        logger.printInfo(Reader.class, "Size " + list.size());

    } catch (IOException ex) {
        ex.printStackTrace();
    }

查询

public List getDetail(int id){
        StringBuilder bf = new StringBuilder();
         bf.append("SELECT ");
         bf.append("'ABC', ");
                ....
        return em.createQuery(bf.toString()) .getResultList();
    }

在这里被困了一个多小时。如有任何帮助或建议,将不胜感激。

共有1个答案

杨骁
2023-03-14

这个问题似乎与查询API有关。

所以在这里,我建议使用like

public List<Object[]> getDetail(int id){
        StringBuilder bf = new StringBuilder();
         bf.append("SELECT ");
         bf.append("'ABC', ");
                ....
        return em.createQuery(bf.toString()) .getResultList(); -- getResultList returns List.
    }

后来呢

List<Object[]> list=new Arraylist<Object[]>();
 .....
 list.addAll(getDetail(id));  -- as we are adding collection here. 
list.add(getDetail(id));
 类似资料: