将对象存储在对象数组中

时间:2019-11-13 00:34:06

标签: java arrays

我正在尝试将受训者对象与培训课程对象链接,但是显示此错误:TrainingSession cannot be converted to TrainingSession[](在最后一行)。

我不能使用数组列表或类似的东西,因为我必须遵循分配中的UML图。我使用以下变量sessionNametraineeNosessionNo来了解我正在处理的是哪个受训者和培训课程。

public class Trainee extends Person {

    private TrainingSession [] ST;

    public TrainingSession [] getST() {
        return ST;
    }

    public void setST(TrainingSession [] ST) {
        this.ST = ST;
    }
}

public class TrainingSession {

    private int trainingSessionID;

    public int getTrainingSessionID() {
        return trainingSessionID;
    }

    public void setTrainingSessionID(int trainingSessionID) {
        this.trainingSessionID = trainingSessionID;
    }
}

public class TMS2 {

    public static void main(String[] args) 
            throws FileNotFoundException, ParseException {

        File file = new File("input.txt");
        Scanner read = new Scanner(file);
        Trainee [] trainee = new Trainee[15];

        int traineeID = read.nextInt();
        int trainingSessionID = read.nextInt();

        String sessionName = TrainingSession(trainingSession, trainingSessionID);
        int traineeNo = TraineeNo( trainee,  traineeID);
        int sessionNo = SessionNo( trainingSession,  sessionName);

        trainee[traineeNo].setST(trainingSession[sessionNo]);
    }
}

1 个答案:

答案 0 :(得分:0)

trainingSession[sessionNo]TrainingSession对象,而不是数组。

也许您正在寻找类似...

trainee[traineeNo].setST(new TrainingSession[] { trainingSession[sessionNo]});