有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将复杂对象添加到对象问题列表中

我尝试为java web项目创建此对象的列表:

package com.example.data;

import java.util.HashMap;

public class StudentGroup {

private String fullName;
private HashMap<Lesson, Integer> lessonsNumber;


public StudentGroup() {
    lessonsNumber = new HashMap<>();
}

public StudentGroup(String fullName, HashMap<Lesson, Integer> lessonsNumber) {
    this.fullName = fullName;
    this.lessonsNumber = lessonsNumber;
}

public String getFullName() {
    return fullName;
}

public void setFullName(String fullName) {
    this.fullName = fullName;
}

public HashMap<Lesson, Integer> getLessonsNumber() {
    return lessonsNumber;
}

public void setLessonsNumber(HashMap<Lesson, Integer> lessonsNumber) {
    this.lessonsNumber = lessonsNumber;
}

public void addLesson(Lesson lesson, int number){
    this.lessonsNumber.put(lesson, number);
}

@Override
public String toString() {
    return "StudentGroup{" +
            "fullName='" + fullName + '\'' +
            ", lessonsNumber=" + lessonsNumber.toString() +
            '}';
}

}

内部jsp页面

以下是我的jsp代码:

    List<StudentGroup> studentGroupList = new ArrayList<>();

for (int i = 0; i < Integer.parseInt(request.getParameter("nostudentgroup")); i++) {
    StudentGroup tempstd = new StudentGroup();
    tempstd.setFullName(request.getParameter("studentgroup" + i));

    HashMap<Lesson, Integer> t = new HashMap<>();
    for (int j = 0; j < Integer.parseInt(request.getParameter("nosubject" + i)); j++) {
        out.println(request.getParameter("stgrpsubject" + i + "-" + j));
        for (int k = 0; k < lessons.size(); k++) {
            if(lessons.get(k).getName().equals(request.getParameter("stgrpsubject" + i + "-" + j))){

                t.put(lessons.get(k), Integer.parseInt(request.getParameter("subjecttime" + i + "-" + j)));
                sum += Integer.parseInt(request.getParameter("subjecttime" + i + "-" + j));
            }
        }
    }


    tempstd.setLessonsNumber(t);
    out.println("<br>"+ tempstd.toString() +"<br>");
    studentGroupList.add(tempstd);
}

这家公司倒闭了。println工作正常,可以正确打印tempstd对象。 但是列表(studentGroup)包含所有索引的空hashmap。 有什么问题吗。添加方法

谢谢你的建议


共 (0) 个答案