gpt4 book ai didi

java - 将学生分配到类(class)中

转载 作者:行者123 更新时间:2023-12-02 04:41:38 25 4
gpt4 key购买 nike

线程“main”中出现异常 javalangIndexOutOfBoundsException在此处输入代码类(class)类别

public class Course implements Serializable {
int id;
String name;
Subject subjects;
Teacher teachers;
List <User> students;
List<Student> listOfStudentss = new ArrayList<Student>();
public int getId() {
return id;
}

public List<Student> getListOfStudentss() {
return listOfStudentss;
}

public void setListOfStudentss(List<Student> listOfStudentss) {
this.listOfStudentss = listOfStudentss;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

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

public Subject getSubjects() {
return subjects;
}

public void setSubjects(Subject subjects) {
this.subjects = subjects;
}

public Teacher getTeachers() {
return teachers;
}

public void setTeachers(Teacher teachers) {
this.teachers = teachers;
}

public List<User> getStudents() {
return students;
}

public void setStudents(List<User> students) {
this.students = students;
}

public Course(int id, String name) {
this.id = id;
this.name = name;
this.subjects = subjects;
this.teachers = teachers;
this.students = students;
this.listOfStudentss = listOfStudentss;
}

@Override
public String toString() {
return (" id: "+id+" name: " + name + "\n");
}


}

类(class)学生


import java.io.Serializable;

public class Student extends User implements Serializable {
String name;
String surname;
String group;

public String getName() {
return name;
}

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

public String getSurname() {
return surname;
}

public void setSurname(String surname) {
this.surname = surname;
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}

public Student(int id, String login, String password, String name, String surname, String group) {
super(id, login, password);
this.name = name;
this.surname = surname;
this.group = group;
}


@Override
public String getUserData() {
return null;
}
public String toString() {
return (" id: "+id+" login: " + login + " password: " + password+" name: "+name+" surname: "+surname+" group: "+group+"\n");
}

}

 List<Admin> listOfAdmins = new ArrayList<Admin>();
List<Student> listOfStudents = new ArrayList<Student>();
List<Teacher> listOfTeachers = new ArrayList<Teacher>();
List<Subject> listofSubjects = new ArrayList<Subject>();
List<Course> listofCourses = new ArrayList<Course>();

Scanner scan222 = new Scanner(System.in);
System.out.print(" ENTER ID OF COURSE: ");
listofCourses.forEach(System.out::print);
int idCourse = scan222.nextInt();
for (int i = 0; i < listofCourses.size(); i++) {
if (listOfCourses.get(i).getId()==(idCourse)) {
System.out.print(" ENTER ID OF STUDENT: ");
listOfStudents.forEach(System.out::print);
int studentID = scan222.nextInt();
for (int j=0; j<listOfStudents.size(); j++)
if (listOfStudents.get(j).getId()==(studentID)) {
//error in this line listofCourses.get(idCourse).listOfStudentss.add(listOfStudents.get(studentID));
}
}
}
listofCourses.get(idCourse).listOfStudentss.forEach(System.out::print);
listofCourses.forEach(System.out::print);

线程“main”中出现异常 java.lang.IndexOutOfBoundsException:索引 1 超出长度 1 的范围 在 java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) 在 java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) 在 java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) 在 java.base/java.util.Objects.checkIndex(Objects.java:372) 在 java.base/java.util.ArrayList.get(ArrayList.java:458) 在 RemR.main(RemR.java:93)

最佳答案

您正在混淆两个不同的东西:类(class) ID 及其在 listOfCourses 列表中的索引。

据推测,您已为第一门类(class)指定了 id“1”,但其在 listOfCourses 列表中的索引为“0”。该错误告诉您,您正在向 listOfCourses 对象询问索引“1”处的对象(这是第二个对象,因为 java 从 0 开始计数),但列表仅包含单个对象,因此会发生异常.

关于java - 将学生分配到类(class)中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56513202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com