从ArrayList中删除学生ID,并显示添加到arrayList中的所有课程

时间:2016-12-17 21:58:05

标签: java arraylist

以下是应删除在主testClass中运行的学生ID的情况。但是,不会从arrayList中删除任何内容。我认为' findIdNum'应该以某种方式在“其他”中使用'声明部分代码,但我不知道如何。

主要问题:如何从arrayList中删除ID,这样当我去录取并查看所有学生时,删除的那个不应该弹出显示。

android.accounts.Account

主testClass中的另一个错误是只显示来自userInput的课程的名字,而不是所有课程。

主要问题:如果在MAT2000和MAT3000中的userInput类型(而不是MAT2000和MAT3000)都应该显示而不是单独使用MAT2000,我怎样才能从arrayList中显示所有课程。

   case 1: //Drop the student (BUG101: NOTHING HAPPENS)

            String findIdNum = GetData.getString("Enter Student ID that needs to be dropped.");
            db.search(findIdNum);
            if(!db.inList())
                JOptionPane.showMessageDialog(null, "The ID: " + findIdNum + " is not in the databse.");       
            else
            {                  
                 int index = db.getIndex();                                                                                       
                 db.add(db.remove(index));
                 JOptionPane.showMessageDialog(null,"Student" + findIdNum + " removed from the system.");                                         
            }     

   break;

以下是数据库类

case 2:                         

      ArrayList getList = db.getList();                                                                                                              
      ArrayList getCourseList = db.getCourseList(); 

      if(getCourseList.isEmpty())                                                                                                 
         JOptionPane.showMessageDialog(null, "No courses in the cart.");                                                                                                
      else                                                                                            
         {                                                                                                        
         String x = "";                                                                                                                                                                                                                                                                            
         int length = db.getSize();                                                                                                       
         try                                                                                              
            {                                                                                                  
            for(int i = 0; i < length; i++ ) 

            {                                                                                                                                                                                                                                                                     
             StudentCourse    sq = (StudentCourse)getCourseList.get(i);                                                                                                                    
             StudentAccounts q = (StudentAccounts)getList.get(i);  

             x += "Student ID:\t"  + q.getStudent().getId()+                                                                                                          
                  "\nName:\t"      + q.getStudent().getName().getFirst()+ " 
                                   "+q.getStudent().getName().getLast() +                                                                                                                                
                  "\nDate:\t"        + df.format(now)+                                                                                                         
                  "\nCourses:\t"   + sq.getCourse() + "\n";   

            }                                                                                                   
            display(x, "All Students" , JOptionPane.INFORMATION_MESSAGE);                                                                                                   
            }                                                                                                      
        catch (IndexOutOfBoundsException e){}                                                                                               
       }                                                                                                         
   break;

以下是班级学生

import java.util.ArrayList;


public class DataBase 
{
        ArrayList <StudentAccounts> list;
        ArrayList <StudentCourse> courseList;

        StudentAccounts sa ;
        StudentCourse sc;
        int index;
        boolean found = false;

        DataBase()
        {
                list = new ArrayList<StudentAccounts> ();                                                       //Inserts a new Student acount into the ArrayList
                courseList = new ArrayList <StudentCourse> ();
        }

        ArrayList getList()                 {return list;}
        ArrayList getCourseList()           {return courseList; } 

        StudentCourse getCourses()          {return sc;}    
        StudentCourse removeCourse(int d)   {return courseList.remove(d); }
        StudentAccounts getStudent()        {return sa;}
        StudentAccounts remove(int d)       {return list.remove(d); }  

        boolean inList()                    {return found;}                                                                                  //Looks in the ArrayList  
        boolean isEmpty()                   {return list.isEmpty();}  
        int getIndex()                      {return list.indexOf(sa);}      
        int getSize()                       {return list.size();}                                                                             // return the amount of strings in the Array     
        int getCourseSize()                 {return courseList.size(); }

        void add(StudentAccounts s)         {list.add(s);}         
        void add(StudentCourse sc)          {courseList.add(sc);}


         void search(String key){
                  found = false;
                  int i = 0;      
                  while (!found && i < getSize() )
                  { 
                             StudentAccounts sl = list.get(i);
                             if(sl.getStudent().getId().equalsIgnoreCase(key))
                             {                    
                                 sa = sl;
                                 found = true;      
                                 index = i;
                             }
                             else{}
                 i++;
                 }
         }        
}

以下是StudentAccounts类

package student;

//This class does 1 task, obtains student information.

public class Student
{                       
        private Name name;
        private String idNUM;
        StudentCourse course;

        public Student(Name n, String  idNum)
        {
                    this.name    = n;
                    this.idNUM = idNum;              
        }

        public void changeIdNumber(String id)
        {
                    idNUM = id;
        }

        public Name getName()                     {return name; }
        public String getId()                     {return idNUM; }            
}

以下是StudentCourse课程

    public class StudentAccounts 
    {         
        public Student stud; 

        public StudentAccounts (Student s) {this.stud = s; }           
        public Student getStudent()        {return stud; }
    }

1 个答案:

答案 0 :(得分:3)

这条线意味着什么? db.add(db.remove(index));

您正在删除它,然后重新添加它,这就是为什么它不会被删除。

Q2:

你在这里迭代什么

for(int i = 0; i < length; i++ ) 

学生?

但是你做了

StudentCourse    sq = (StudentCourse)getCourseList.get(i); 

因此,如果i是学生索引,那么上面的行是否有意义?