0

包含代码和模块的程序类

import java.util.ArrayList;

public class Programme {

    private ArrayList<String> modules = new ArrayList<String>();
    private String ProgrammeCode;

    public Programme(String ProgrammeName) {
        this.ProgrammeCode = ProgrammeName;
        if (modules.size() > 0) {
            modules.clear(); //clear if something in modules
        }
    }

    public Programme(String programmeCode, ArrayList<String> moduleList) {
        this.ProgrammeCode = programmeCode;
        modules = moduleList;
    }

    public void addModule(String name) {
        if (name != null && !name.equals("")) {
            modules.add(name);
        }
    }

    public String getProgrammeCode() {
        return ProgrammeCode;
    }

    public ArrayList<String> getModules() {
        return modules;
    }

    public int getModuleCount() {
        return modules.size();
    }
}

这是在读取 csv 文件并将它们存储到类型 Programme 中的类中,但是我在将字符串添加到模块中时遇到了问题。

public ArrayList<Programme> loadProgrammes() {
    ArrayList<String> modules = new ArrayList<String>(); //.... modules are being read from
    //a csv file and have done multiple tests and its working correctly   
    modules.add(".....");

    //PROBLEM HERE     
    programmes.add(new Programme(programmeStrings.get(i).get(0), modules));
    //this displays as i want it to e.g. when added:3:[MA4102, CS4092, CS4162] 
    System.out.print("when added:" + programmes.get(i).getModuleCount() + ":" + modules);

    //where as this doesn't work e.g.  //Finished list:3 //LM023 0 []
    //LM051 0 [] //LM110 0 [] 
    System.out.println("\nFinished list:" + programmes.size());
    for (int t = 0; t < programmes.size(); t++) {
        System.out.println(programmes.get(t).getProgrammeCode() + " " + programmes.get(t).getModuleCount() + " "
                + programmes.get(t).getModules());
    }

    return programmes;
}

我不确定出了什么问题。为什么不将模块添加到程序对象

4

0 回答 0