指定学生参加课程

时间:2019-06-09 08:48:01

标签: java arraylist

线程“ main”中的异常javalangIndexOutOfBoundsException 在此处输入代码 课程课

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");
    }


    }

班级学生


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);

线程“主”中的异常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)

1 个答案:

答案 0 :(得分:0)

您要混淆两件事:课程ID及其在listOfCourses列表中的索引。

大概您给第一门课程的ID为“ 1”,但它在listOfCourses列表中的索引为“ 0”。错误告诉您,您正在向listOfCourses对象请求索引为“ 1”的对象(第二个对象,因为Java从0开始计数),但是列表仅包含单个对象,因此会发生异常