sweetstar86 2016-02-27 14:14 采纳率: 60%
浏览 3215

新手关于使用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条回答

  • Exploring1024 2016-02-27 15:03
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序