Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用XML序列化具有另一个ArrayList的类的ArrayList_Java_Xml_Serialization_Arraylist - Fatal编程技术网

Java 使用XML序列化具有另一个ArrayList的类的ArrayList

Java 使用XML序列化具有另一个ArrayList的类的ArrayList,java,xml,serialization,arraylist,Java,Xml,Serialization,Arraylist,我正在尝试序列化一个类的ArrayList,该类使用另一个ArrayList。能做到吗? 以下是我的代码片段: public class Model { private ArrayList<Student> students; private ArrayList<Module> modules; //... public void saveStudentsXML() throws IOException { XMLEnc

我正在尝试序列化一个类的ArrayList,该类使用另一个ArrayList。能做到吗?
以下是我的代码片段:

public class Model {
    private ArrayList<Student> students;
    private ArrayList<Module> modules;

    //...

    public void saveStudentsXML() throws IOException {
        XMLEncoder encoder=new XMLEncoder(new BufferedOutputStream(new FileOutputStream("students.xml")));
        encoder.writeObject(students);
        encoder.close();
    }
    public void loadStudentsXML() throws IOException {
        XMLDecoder decoder=new XMLDecoder(new BufferedInputStream(new FileInputStream("students.xml")));
        students=(ArrayList<Student>)decoder.readObject();
        decoder.close();
    }
    //this works fine

    public void saveModulesXML() throws IOException {
        XMLEncoder encoder=new XMLEncoder(new BufferedOutputStream(new FileOutputStream("modules.xml")));
        encoder.writeObject(modules);
        encoder.close();
    }
    public void loadModulesXML() throws IOException {
        XMLDecoder decoder=new XMLDecoder(new BufferedInputStream(new FileInputStream("modules.xml")));
        modules=(ArrayList<Module>)decoder.readObject();
        decoder.close();
    }
    //this does not

}
当我查看XML时,似乎没有关于Enrolled Students ArrayList的任何信息。

编辑:我已经为除Enrolled Students ArrayList之外的所有实例变量创建了setter和getter。据我所知,不能为ArrayList创建setter方法,对吗?有没有其他方法可以让XMLEncoder对其进行编码?

好的,事实证明,您可以为ArrayList创建setter,这是我以前从未需要做的事情,并且愚蠢地认为这是不可能的。
在意识到我没有为ArrayList创建getter或setter之后,我尝试了以下方法:

public ArrayList<Student> getEnrolledStudents() {
    return enrolledStudents;
}
public void setEnrolledStudents(ArrayList<Student> enrolledStudents) {
    this.enrolledStudents = enrolledStudents;
}
public ArrayList getEnrolledStudents(){
返回注册学生;
}
public void setEnrolledStudents(ArrayList enrolledStudents){
this.enrolledStudents=已注册学生;
}
这就是我需要添加到模块类中的全部内容,之后它工作得很好

public ArrayList<Student> getEnrolledStudents() {
    return enrolledStudents;
}
public void setEnrolledStudents(ArrayList<Student> enrolledStudents) {
    this.enrolledStudents = enrolledStudents;
}