繁体   English   中英

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

[英]How to remove object from an arraylist

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

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

                                              }
                                                                    }
                                                                            }

这就是删除功能:

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;

删除功能不会给我任何代码错误,但是当用户选择要删除的课程时,它会给我构建失败

对不起,这似乎很复杂,但我希望任何人都有解决方案。 谢谢

您删除课程的标准是什么? 通常,您可以使用 for 循环搜索 arraylist:

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

你可以保存对象-->需要删除

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类中删除Course list ,并从Student类中删除Student list 您仍然可以将它们作为实例字段(甚至作为静态字段)进行访问,但是将它们放在它们所在的位置对我来说真的没有意义。

  • 首先检查课程是否正在教授
  • 如果是这样,清除参加该课程的学生(不再可用)
  • 还从参加课程的学生中删除课程
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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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