[英]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));
}
}
hashCode
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.