2 sinat 33645256 sinat_33645256 于 2016.02.27 22:14 提问

新手关于使用java集合造成的java.lang.StackOverflowError

如图的题目,图片说明
据此,我写了三个类,第一个Student类,代码:

 package com.wms.StudentManager2;

import java.util.List;

public class Student {

    private String name;
    private int age;
    private List crList;
    public List crList() {
        return crList;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public List getList() {
        return crList;
    }
    public void setList(List crList) {
        this.crList = crList;
    }
    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;

    }
    public Student() {}
    @Override
    public String toString() {
        if(crList==null){
            return "Student [name=" + name + ", age=" + age + "该生从未选课";
        }
        return "Student [name=" + name + ", age=" + age + ", course=" + crList + "]";
    }



}

第二个Course类,代码:

package com.wms.StudentManager2;

import java.util.List;

public class Course {

    private String name;
    private int score;
    private List stuList;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public List getList() {
        return stuList;
    }

    public void setList(List stuList) {
        this.stuList = stuList;
    }

    public Course(String name, int score,List stuList) {
        super();
        this.name = name;
        this.score = score;
        this.stuList = stuList;
    }

    public Course() {}

    @Override
    public String toString() {
        return "Course [name=" + name + ", score=" + score + ", student=" + stuList + "]";
    }


}


第三个Manager类,代码:

 package com.wms.StudentManager2;

import java.util.ArrayList;
import java.util.List;

public class Manager {

    public static void main(String[] args) {

        Student s1 = new Student("张小三", 20);
        Student s2 = new Student("李小思", 19);
        Student s3 = new Student("王五", 18);
        Student s4 = new Student("赵晓丽", 19);
        Student s5 = new Student("黎明", 20);
        List<Student> stuList1 = new ArrayList<>();
        List<Student> stuList2 = new ArrayList<>();
        List<Student> stuList3 = new ArrayList<>();
        stuList1.add(s1);
        stuList1.add(s2);
        stuList1.add(s3);
        stuList1.add(s4);

        stuList2.add(s1);

        stuList3.add(s1);

        Course c1 = new Course("高等数学", 5, stuList1);
        Course c2 = new Course("线性代数", 3, stuList2);
        Course c3 = new Course("离散数学", 4, stuList3);

        List<Course> crList1 = new ArrayList();
        List<Course> crList2 = new ArrayList();
        List<Course> crList3 = new ArrayList();
        List<Course> crList4 = new ArrayList();
        crList1.add(c1);
        crList1.add(c2);
        crList1.add(c3);

        crList2.add(c1);

        crList3.add(c1);

        crList4.add(c1);

        s1.setList(crList1);
        s2.setList(crList2);
        s3.setList(crList3);
        s4.setList(crList4);
        System.out.println(c1);

    }

}

如果我不给student setCourseList就可以正常,但是一旦set之后就会报java.lang.StackOverflowError, at java.lang.StringBuilder.(Unknown Source)
at com.wms.StudentManager2.Student.toString(Student.java:40)
at java.lang.String.valueOf(Unknown Source)等等错误。实在不知道问题出在哪里,希望大家帮忙看下。

3个回答

caozhy
caozhy   Ds   Rxr 2016.02.27 23:42

stulist和crlist相互引用对方,导致你调用tostring的时候无限递归。去掉两者之一对对方的引用。

caozhy
caozhy 回复sweetstar86: 重写任何一侧的toString,不再读取list里的toString
接近 2 年之前 回复
sinat_33645256
sinat_33645256 那应该怎么多对多关联呢?
接近 2 年之前 回复
Mr_dsw
Mr_dsw   Ds   Rxr 2016.02.27 23:04

提示的是40行有问题,在toString函数

Mr_dsw
Mr_dsw   Ds   Rxr 2016.02.27 23:03

提示的是40行有问题,在toString函数

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!