尝试将对象添加到链表(java util)时遇到此问题 我有一个Doctor和Patient类,都继承自抽象类Person
public abstract class Person {
private LinkedList<Appointment> scheduledAppointments = new LinkedList<Appointment>();
//irrelevant code
public boolean addAppointment(Appointment appointment){
return this.scheduledAppointments.add(appointment);
}
参与者属性在链接列表中具有医生和患者。
public class Appointment {
private ArrayList<Person> participants = new ArrayList<Person>();
...
public Appointment(Person doctor, Person patient, int day, int month, int year, double startHour, Procedure procedure) {
this.participants.add(doctor);
this.participants.add(patient);
...
}
public void addAppointmentToParticipants(){
for (Person person : participants) {
person.addAppointment(this);
}
}
the problem occurs when i'm trying to add the Appointment to each of the participants:
Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate Appointment.toString()
当我调试它时,我可以看到该异常发生在LinkedList.java上, 特别是在size ++行上
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
我不明白为什么我会遇到这个问题,“ toString”与它有什么关系...