简体   繁体   English

添加到链接列表时出现 java.lang.stackoverflowerror

[英]java.lang.stackoverflowerror when adding to a Linked-List

I'm getting this problem while trying to add an object to a linkedlist (java util) I've a Doctor and Patient class, both inherit from the abstract class Person我在尝试将object添加到链表(java util )时遇到了这个问题

public abstract class Person {
    private LinkedList<Appointment> scheduledAppointments = new LinkedList<Appointment>();

    //irrelevant code

    public boolean addAppointment(Appointment appointment){
        return this.scheduledAppointments.add(appointment);
    }

the participants attribute has a doctor and a patient in the linked list.参与者属性在链表中有一个医生和一个病人。

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()当我尝试将约会添加到每个参与者时会出现问题: Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate Appointment.toString() Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate Appointment.toString()

when I'm debugging it, i can see that the exception occurs on the LinkedList.java, particularly on the size++ line当我调试它时,我可以看到异常发生在 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++;
    }

I don't understand why i'm haing this problem, and what does "toString" has to do with it...我不明白为什么我会遇到这个问题,以及“toString”与它有什么关系......

Based on the toString() in the error message, it looks like whatever toString() you have in Appointment embeds the string representation of Person , but whatever toString() you have in Person does the same for Appointment .根据错误消息中的toString() ,看起来您在Appointment中拥有的任何toString()都嵌入了Person的字符串表示,但在Person中拥有的任何toString()都对Appointment做同样的事情。 This would lead to a StackOverflowError anytime you call toString() , because the string would continually expand (and the relationship is bi-directional here).任何时候调用toString()都会导致StackOverflowError ,因为字符串会不断扩展(并且这里的关系是双向的)。

This would also make printing out your LinkedList impossible without encountering the error.这也将使您无法在不遇到错误的情况下打印出LinkedList

The solution is to make the toString() of one of these classes not embed the other's toString() .解决方案是使这些类之一的toString()嵌入另一个类的toString() Better yet, you should rethink your model and determine whether you really need this bi-directional relationship.更好的是,您应该重新考虑您的 model 并确定您是否真的需要这种双向关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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