简体   繁体   中英

Using two different objects of the same type in different classes in Java

I am writing a program in Java which is supposed to be like a student administration with different lists of students, subjects, teachers and so on. The program shall among others include one general subjectlist and one subjectlist for each student. The problem is that when I create two different subjects and add it to the general subjectlist and later on find one of these and add it to the student subjectlist, the student subjectlist contains both of these subjects.

I have searched the web but it is not so easy to know what to look for!

I am writing the datastructure myself.

My code looks something like this:

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
    }

}

Any help would be much appreciated!

EDIT:

This is the find and addToList methods:

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;
}

We really need the rest of the code to really confirm what is the problem, but from what you have posted, I will take an educated guess:

As you are implementing your own Linked List, that implementation has leaked through to the Subject class:

public class Subject() {
    Subject next; 
    //constructor with parameters
}

Here - any instance of Subject you create, if it is part of a list it will hold a reference to the next item in the Linked list.

For example, in the code you posted:

Subject subject1 = new Subject(parameters);
Subject subject2 = new Subject(parameters);

subjectlist.addToList(subject1);
subjectlist.addToList(subject2);

I assume that for your linked list implementation to be working, the SubjectList first variable points to subject1 , and then subject1 s next variable points to subject2 .

This causes obvious problems when you try to us subject1 in any other list, as it still holds an reference to subject2 .

If you really have/need to create your own implementation of a LinkedList for the particular project, then you could create a wrapper class, that has the next variables and a reference to Subject - that way, the LinkedList implementation doesn't leak through to the underlying objects at all.

eg something like:

public class SubjectWrapper {
    Subject subject;
    SubjectWrapper next; 
    //constructor with parameters
}

You don't need expose this underlying abstraction, and still pass subject1 to the addToList method, but in the method stick it inside the wrapper class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM