我正在用 Java 编写一个程序,它应该像一个学生管理,具有不同的学生、科目、教师等列表。除其他外,该计划应包括一个普通科目列表和每个学生的一个科目列表。问题是,当我创建两个不同的主题并将其添加到一般主题列表中,然后找到其中一个并将其添加到学生主题列表时,学生主题列表包含这两个主题。
我已经在网上搜索过,但要知道要寻找什么并不容易!
我正在自己编写数据结构。
我的代码看起来像这样:
public class Subject() {
Subject next;
//constructor with parameters
}
public class Subjectlist() {
private Subject first;
//methods for adding to list, deleting, find and so on
}
public class Participation {
Subjectlist subjects;
public Participation() {
subjects = new Subjectlist();
}
}
public class Student() {
Participation participation;
public Student(paramters) {
participation = new Participation();
}
public class mainclass() {
public static void main(String [] args) {
Subjectlist subjectlist = new Subjectlist();
Studentlist students = new Studentlist();
Student student = new Student(parameters);
students.addToList(student);
Subject subject1 = new Subject(parameters);
Subject subject2 = new Subject(parameters);
subjectlist.addToList(subject1);
subjectlist.addToList(subject2);
Subject subject = subjectlist.find(subjectid); //Finds the subject with an ID given in the constructor
student.participation.subjects.addToList(subject);
//Now student.participation.subjects contains both subject1 and subject2
}
}
任何帮助将非常感激!
编辑:
这是 find 和 addToList 方法:
public String addToList(Subject new) {
Subject pointer = first; //Subject first is declared in the class
if(new == null) {
return "The subject was not added.";
}
else if (first == null) {
first = new;
return "The subject was added";
}
else {
while ( pointer.next != null )
pointer = pointer.next;
pointer.next = new;
return "The subject was added";
}
}
public Subject find(String subjectid) {
Subject found = null;
Subject pointer = first;
while (pointer != null) {
if (pointer.getSubjectID().equals(subjectid)) {
found = pointer;
}
pointer = pointer.next;
}
return found;
}