简体   繁体   中英

How to remove object from an arraylist

its gonna seem complicated but I want to make a function that removes an object from the ArrayList of an object that is in another ArrayList let me simplify it, there is a class with objects (students) and every student has an ArrayList (studentCourses). I want to make a function that removes a course and when the course is removed it is also removed from the student courses list

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

that's the course class:


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

and that's the function that registers the student in the course:

public static void master_register_courses_student(){

                           if (Course.courseList.isEmpty()){
                                    System.out.println("There are no courses currently to assign a student to\n 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 to\n 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));
                                                    }

                                              }
                                                                    }
                                                                            }

and that's the remove function:

public static void removeobject(){
                            System.out.println("Choose what you want to remove.");
                            System.out.println("1.Courses\t2.Students\t3.Tutors");
                            int i=in.nextInt();      
                            switch(i){
                            case 1:
                            if(Course.courseList.isEmpty()){
                                System.out.println("Course list is empty\nTry 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;

the remove function doesn't give me any errors in code but when the user chooses the course to remove it gives me build failure

I'm sorry it seems complicated but I hope anyone has a solution for it. and thanks

At which criteria you delete a course? In general you can search through the arraylist with a for loop:

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

you could save the object--> needstobedeleted

for(int j=0;j<Student.studentlist.size();j++){
if(Student.studentlist.get(j).getCourseName()==needstobedeleted.getCourseName()||tobedeleted){
Student.studentlist.remove(j);
}

}

Did I understand your question right? Please tell me But if i did this could help

Here is how I would do it. I am presuming you would have a List<Student> and a List<Course> accessible. You may have to modify this to fit your needs. I would also recommend you remove the Course list from the Course class and and the Student list from the Student class. You can still make them accessible as instance fields (even as static fields) but it really makes no sense to me to have them where they are.

  • first check to see if the course is being taught
  • if so, clear the students taking that course (no longer available)
  • also remove the course from the Student taking the course
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));
    }
}
  • You will need to ensure equals is overriden correctly based on your criteria for equality. (you may want to override hashCode too for completeness.
  • your classes should have getters and setters implemented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM