java.util.ArrayList无法强制转换为[Ljava.lang.Object

时间:2017-11-27 09:21:50

标签: java list arraylist

把我的头发拉出来了!文本文件中有两个数据。我试图找回它们,但我得到了以下错误

##########  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小时。任何帮助或建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

使用generic types

        List<Details> list = new ArrayList<>();

        try {
            while ((st = br.readLine()) != null) {
                String id = st.substring(92, 100);
                try {
                    list.addAll(getDetail(id)); // add all results
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            br.close();

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

您的getDetail方法应返回转换为详细信息列表的行

public List<Details> getDetail(int id){ 
    // prepare your query
    StringBuilder bf = new StringBuilder();
    bf.append("SELECT ");
    bf.append("'ABC', ");

    // iterate over search results and convert each row into Details
    List<Details> results = new ArrayList<>();
    ResultSet rs = stmt.executeQuery(bf.toString());
    while (rs.next()) {
       results.add(toDetails(rs)); // convrt single row to Details model
    }
    return results;
}

将单result row转换为Details

private Details toDetails(ResultSet rs){
    Details x = new Details();
    x.setType(rs.getString(0));
    ...
    return x;
}

答案 1 :(得分:1)

此问题似乎与Query API有关。

在这里,我建议使用像

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));

原始代码。

有关详情,请参阅现有的SO post