如何从数组列表中删除对象



这看起来很复杂,但我想写一个函数来从数组列表中删除另一个数组列表中对象的对象让我简化一下,有一个类的对象(学生)每个学生都有一个数组列表(学生课程)。我想创建一个函数来删除课程,当课程被删除时,它也会从学生课程列表中删除

public class Student extends User{
ArrayList<Course> StudentcoursesTaken=new ArrayList<>();
public static int studentsNO; //var. to help in assigning an id to every student
protected int NoOfCoursesTaken=StudentcoursesTaken.size();
private int studentId;
private String studentName;
protected static ArrayList<Student> studentList = new ArrayList<>();

这是课程类别:


public class Course  {
private int courseId;
private String courseName;
private String courseDescription;
private int maxStudents;
private Lecturer tutor; //aggergation relationship
protected ArrayList<Student> course_students = new ArrayList<>();
protected static ArrayList<Course> courseList = new ArrayList<>();

这是注册学生的函数

public static void master_register_courses_student(){
if (Course.courseList.isEmpty()){
System.out.println("There are no courses currently to assign a student ton Try to add a new course");
}else if((Course.courseList.size())>=1)  {
System.out.println("choose the course");
Course.displayCourses();
int r=in.nextInt();
if ((Course.courseList.get(r-1)).course_students.size()==(Course.courseList.get(r-1)).getMaxStudents()){
System.out.println("The course is complete n a new student cannot be added");
}else{
if(Student.studentList.isEmpty()){
System.out.println("There is no student currently to assign a course ton Try to add a new student");
}else{
System.out.println("choose the student you want to add to the course");
Student.displayStudent();
int n=in.nextInt();
(Course.courseList.get(r-1)).course_students.add(Student.studentList.get(n-1));
(Student.studentList.get(n-1)).StudentcoursesTaken.add(Course.courseList.get(r-1));
}
}
}
      }

这就是remove函数:

public static void removeobject(){
System.out.println("Choose what you want to remove.");
System.out.println("1.Coursest2.Studentst3.Tutors");
int i=in.nextInt();      
switch(i){
case 1:
if(Course.courseList.isEmpty()){
System.out.println("Course list is emptynTry to add new courses");
}else{
System.out.println("Which course would you like to remove?");
Course.displayCourses();
int c=in.nextInt();                                                   
if(c>Course.courseList.size()){
System.out.println("invalid choice");                                   
}else{
Course.courseList.remove((c-1));
for(int f=0;f<Student.studentList.size();f++){
(Student.studentList.get(f)).StudentcoursesTaken.removeIf(p->p.getCourseName().equals((Course.courseList.get(c-1)).getCourseName()));
}    
System.out.println("A course removed successfully");
}}
break;

remove函数在代码中没有给我任何错误但是当用户选择要删除的课程时,它给了我构建失败

我很抱歉这看起来很复杂,但我希望任何人都有一个解决方案。由于

按什么标准删除课程?一般来说,你可以用for循环遍历数组列表:

ArrayList<Course> StudentcoursesTaken=new ArrayList<>();
public void removeObject(Course course){
Course needstobedeleted;
for(int i=0;i<StudentcoursesTaken.size();i++){
if(StudentcoursesTaken.get(i).getCourseName()==course.getCourseName()){
needstobedeleted = StudentcoursesTaken.get(i);
//this is other way
String tobedeleted = StudentcoursesTaken.get(i).getCourseName();
StudentcoursesTaken.remove(i);
}

你可以保存对象——>needstobedeleted

for(int j=0;j<Student.studentlist.size();j++){
if(Student.studentlist.get(j).getCourseName()==needstobedeleted.getCourseName()||tobedeleted){
Student.studentlist.remove(j);
}
}
我理解你的问题对吗?请告诉我但如果我这样做了,可能会有帮助

我是这样做的。我假设你有一个List<Student>和一个List<Course>。您可能需要修改它以适应您的需要。我还建议您将Course listCourse类中移除,并将Student listStudent类中移除。你仍然可以使它们作为实例字段访问(甚至作为静态字段),但对我来说,把它们放在它们所在的地方真的没有意义。

  • 首先检查课程是否正在教授
  • 如果是,清除该课程的学生(不再可用)
  • 还将该课程从正在学习该课程的学生中删除
public static boolean removeCourse(List<Student> students, List<Course> courses, Course course) {
if (courses.contains(course)) {
course.course_students.clear();  // course dropped so remove all students 
students.removeIf(student->student.getCourse().equals(course));
}
}
  • 您需要确保根据您的相等标准正确覆盖equals。(你可能也想覆盖hashCode的完整性。
  • 你的类应该实现getter和setter。

相关内容

  • 没有找到相关文章

最新更新