下面是一个应该删除学生ID的案例,在主测试类中运行。但是并没有从 arrayList 中删除任何内容。我认为“findIdNum”应该以某种方式在代码的“else”语句部分中使用,但我不知道如何使用。
主要问题:如何从 arrayList 中删除 ID,以便当我去招生并查看所有学生时,被删除的 ID 不应在显示中弹出。
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;
主 testClass 中的另一个错误是,只有来自用户输入的类(class)的名字才会显示,而不是所有类(class)。
主要问题:在用户输入(即 MAT200,然后 MAT3000)后,我如何才能从 arrayList 显示所有类(class),MAT2000 和 MAT3000 都应该显示,而不是单独显示 MAT2000。
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;
下面是DataBase类
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++;
}
}
}
下面是学生类(class)
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; }
}
下面是 StudentAccounts 类
public class StudentAccounts
{
public Student stud;
public StudentAccounts (Student s) {this.stud = s; }
public Student getStudent() {return stud; }
}
下面是StudentCourse类(class)
public class StudentCourse
{
public String sc;
public StudentCourse(String s) {sc = s;}
public String getCourse() {return sc; }
}
最佳答案
这行代码的作用是什么? db.add(db.remove(index));
您正在删除它,然后重新添加它,所以这就是它没有被删除的原因。
第二季度:
你在这里迭代什么
for(int i = 0; i < length; i++ )
学生?
但是你会的
StudentCourse sq = (StudentCourse)getCourseList.get(i);
那么如果 i
是学生索引,上面的行有意义吗?
关于java - 从ArrayList中删除学生ID,并显示添加到ArrayList中的所有类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41203448/