无法转换为 java.util.ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30071072/
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-11-02 16:23:35  来源:igfitidea点击:

Cannot be cast to java.util.ArrayList

javaarraysarraylist

提问by David Lawlor

Trying to write an object to an array and then save to output.data, then read the objects again.

尝试将对象写入数组,然后保存到 output.data,然后再次读取对象。

Writing:

写作:

private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        

    File outFile;
    FileOutputStream fStream;
    ObjectOutputStream oStream;

    try {
        outFile = new File("output.data");
        fStream = new FileOutputStream(outFile);
        oStream = new ObjectOutputStream(fStream);

        oStream.writeObject(arr);

        JOptionPane.showMessageDialog(null, "File Written Successfully");

        oStream.close();
    } catch (IOException e) {
        System.out.println("Error: " + e);
    }
}                  

Reading:

阅读:

private void readBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        

    File inFile;
    FileInputStream fStream;
    ObjectInputStream oStream;

    try {
        inFile = new File("output.data");
        fStream = new FileInputStream(inFile);
        oStream = new ObjectInputStream(fStream);

        //create an array of assessments
        ArrayList <Assessment> xList;
        xList = (ArrayList<Assessment>)oStream.readObject();



        for (Assessment x:xList) {
            JOptionPane.showMessageDialog(null, "Name: " + x.getName() + "Type: " + x.getType() + "Weighting: " + x.getWeighting());
        }

        oStream.close();
    } catch (IOException e) {
        System.out.println("Error: " + e);
    } catch (ClassNotFoundException ex) {
        System.out.println("Error: " + ex);
    }


}                      

The code comppiles just fine, and the file itself saves alright too. But when I try to read the file nothing happens, and NetBeans says

代码编译得很好,文件本身也保存得很好。但是当我尝试读取文件时没有任何反应,NetBeans 说

"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Lnetbeansswingexample.Assessment; cannot be cast to java.util.ArrayList"

“线程“AWT-EventQueue-0”中的异常 java.lang.ClassCastException: [Lnetbeansswingexample.Assessment; 不能转换为 java.util.ArrayList”

the line of code giving trouble seems to be

给麻烦的代码行似乎是

xList = (ArrayList<Assessment>)oStream.readObject();

Any help would be really appreciated, thanks. Sorry if the answers obvious, pretty new to programming.

任何帮助将不胜感激,谢谢。对不起,如果答案很明显,对编程来说很陌生。

回答by Eran

Based on the exception you got, it looks like oStream.readObject()returns an array of Assessment, not a List. You can convert it to a List:

根据你得到的异常,它看起来像是oStream.readObject()返回一个数组Assessment,而不是一个List。您可以将其转换为List

    List <Assessment> xList;
    xList = Arrays.asList((Assessment[])oStream.readObject());

or if you must use a java.util.ArrayList:

或者如果您必须使用java.util.ArrayList

    ArrayList<Assessment> xList;
    xList = new ArrayList<> (Arrays.asList((Assessment[])oStream.readObject()));