所以我被困住了。我有一个fname lname sNum等学生名单。我正在尝试使用我的删除功能,但它不起作用。我无法弄清楚我的问题在哪里。
public void removeStudent(long sNumber) {
//create student object
Student student = new Student();
//loop through the students
for (int i = 0; i < this.students.size(); i++) {
//display students
System.out.println(this.students.get(i));
//condition if sNumber == sNumber
if(student.getsNumber() == sNumber){
//remove the student from the list
this.students.remove(student);
System.out.println(this.students.get(i));
}
System.out.println("skipped the if statement");
}
/*for(Student student : this.students){
if(student.getsNumber() == sNumber){
this.students.remove(student);
}
}*/
}
这是我称之为方法的地方
case 3:
// delete a student
System.out.print("What is the Students sNumber: s");
long sNum = input.nextLong();
Student chkSNum = new Student();
registry.getStudentBySNumber(sNum);
chkSNum.setsNumber(sNum);
if (registry.getStudentBySNumber(sNum) == chkSNum) {
if (chkSNum.getsNumber() == sNum) {
registry.removeStudent(sNum);
System.out.println(chkSNum);
}
} else {
System.out.println("Sorry no matches");
// System.out.println(sNum);
System.out.println(registry.getStudentBySNumber(sNum));
System.out.println(chkSNum);
}
break;
我的添加学生方法效果很好。
public void addStudent(Student student) {
this.lastSNumber++;
student.setsNumber(this.lastSNumber);
this.students.add(student);
}
答案 0 :(得分:3)
if(student.getsNumber() == sNumber)
将您的号码与新构建的空白Student
对象进行比较。如果你想将它与你正在迭代的那些进行比较,你需要这样的东西:
if(this.students.get(i).getsNumber() == sNumber) {
this.students.remove(i);
}
答案 1 :(得分:0)
您实际上是删除了new studend()
个对象。循环列表如下:
Student to_remove = null:
for(student s: this.students){
if(s.getsNumber() == sNumber){
to_remove = s;
}
if(s!=null) break;
}
if(s!=null)this.students. remove(s);
这不会导致修改 - 异常
答案 2 :(得分:0)
您应该使用迭代器在列表中删除。像
这样的东西 Iterator<Student> it = this.students.iterator();
while (it.hasNext()) {
Student currentStudent = it.next();
if(currentStudent.getsNumber() == sNumber)
{
it.remove();
}
}
答案 3 :(得分:-2)
所以我想出了什么是错的。我的逻辑是愚蠢的。
public void removeStudent(long sNumber) {
//create student object
//Student student = new Student();
Student student = this.getStudentBySNumber(sNumber);
//condition if sNumber == sNumber
if(student == this.getStudentBySNumber(sNumber)){
System.out.println("removing the student from the list");
this.students.remove(student);
displayStudents();
}
}
case 3:
// delete a student
System.out.print("What is the Students sNumber: s");
long sNumInput = input.nextLong();
Student chkSNum2 = new Student();
registry.getStudentBySNumber(sNumInput);
chkSNum2.setsNumber(sNumInput);
if (registry.getStudentBySNumber(sNumInput) != chkSNum2) {
if (chkSNum2.getsNumber() == sNumInput) {
System.out.println("Student deleted...");
registry.removeStudent(sNumInput);
}
} else {
System.out.println("Sorry no matches");
System.out.println(registry.getStudentBySNumber(sNumInput));
System.out.println(chkSNum2);
}
break;
答案 4 :(得分:-2)
removeStudent
显然有些不对劲。首先,您要创建一个新的Student
,然后使用该实例删除。
你应该做的是:
public void removeStudent(long sNumber) {
for (Student s : this.students) {
if (s.getNumber() == sNumber) {
this.students.remove(s);
}
}
}
您也可以使用代码中的for
循环。这是一回事。请记住删除正确的实例。
希望有所帮助。