试图删除学生但不断收到错误

时间:2015-01-21 13:21:51

标签: java methods runtime-error

这是我的.java,包含我的主要方法:

package studentapp;

public class Studentapp {
    public static ArrayList<Student> students = new <Student> ArrayList();
    public static ArrayList snumbers = new ArrayList();
    public static Scanner user = new Scanner(System.in);
    public static int sncount = 0;

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
        students.add(new Student("Kevin", "Swim", "AAS", 3.5));
        students.add(new Student("Kyle", "Swim", "AAS", 2.8));
        students.add(new Student("Bob", "Smith", "AAS", 2.8));
        sncount = 1;
        createArraynum();
        createMenu();
}


private static void addastudent(){
    System.out.println("First Name: " );
    String fname = user.next();
    System.out.println("Last Name: ");
    String lname = user.next();
    System.out.println("Major: ");
    String major = user.next();
    System.out.println("GPA: ");
    double gpa = user.nextDouble();
    Student a = new Student (fname,lname,major,gpa);
    students.add(a);
    a.sNumber = 1234500 + sncount;
    snumbers.add(a.sNumber + sncount);
    sncount ++;
    createMenu();

    }
private static void findaStudent(int input){
    if (snumbers.contains(input)) {
        for(Student i : students){
            if(i.sNumber == input)
            {System.out.println(i.sNumber +" "  + i.firstName + " " + i.lastName +
                     " " + i.major + " gpa: " + i.gPA );

            }
            }
    } else {
       System.out.println("Sorry, The Student with that sNumber does not exist!");
} 
    createMenu();
}


private static void deleteaStudent(int input){

    if (snumbers.contains(input)) {
        for(Student i : students){
            if(i.sNumber == input){
                int number = students.indexOf(i);
                students.remove(number);
                System.out.println(i.sNumber +" "+ i.firstName+" "+i.lastName + " has been deleted!");
            }
            else { System.out.println(" ");}
            }
    } else {
       System.out.println("Sorry, We cannot find a student with that number!");

    }
}
private static void displayStudents(){
    for(Student i : students){
        System.out.println(i.sNumber +" "  + i.firstName + " " + i.lastName +
                     " " + i.major + " gpa: " + i.gPA );
    }
    createMenu();
}
private static void displayNumStudent(){
    System.out.println(students.size());
    createMenu();
}
private static void exit(){
    System.out.println("Thank you, Goodbye");
}
private static void createMenu(){

        System.out.println("1. Add a Student");
        System.out.println("2. Find a Student");
        System.out.println("3. Delete a Student");
        System.out.println("4. Display all Students");
        System.out.println("5. Display the total number of students");
        System.out.println("6. Exit");
        System.out.println("Enter your Selection: ");

        int count = user.nextInt();
        switch (count){
            case 1: addastudent();
                 break;
            case 2: System.out.println("Find student with sNumber S");
                    int number = user.nextInt();
                    findaStudent(number);
                 break;
            case 3: System.out.println("Delete student with sNumber S");
                    int number1 = user.nextInt();
                    deleteaStudent(number1);
                 break;
            case 4:  displayStudents();
                 break;
            case 5:  displayNumStudent();
                 break;
            case 6:  exit();
                 break;
            default : System.out.println("Please Make a valid Selection");
                      createMenu();

        }
}
private static void createArraynum(){
    for(Student i : students){
        i.sNumber = 1234500;
        i.sNumber = i.sNumber + sncount;
        snumbers.add(i.sNumber);
        sncount ++;
    }
}

}

这是我的学生班。

public class Student {
String firstName;
String lastName;
int sNumber = 1234500;
String major;
double gPA;
int count;


public Student(String fName, String lName, String maj, double gpa){
    firstName = fName;
    lastName = lName;
    major = maj;
    gPA = gpa;


}
public String getFirstName(){
    return this.firstName;
}
public String getLastName(){
    return this.lastName;  
}
public void setFirstName(String name){
    this.firstName = name;

}
public void setLastName(String name){
    this.lastName = name;

}
public int getSNumber(){
    return this.sNumber;    
}
public String getMajor(){
    return this.major;

}
public void setMajor(String smajor){
    this.major = smajor;

}
public double getGpa(){
    return this.gPA;

}
public void setGpa(double num){
    this.gPA = num;

}
@Override
public String toString(){
    return firstName;

}

}

一切似乎都按照我想要的方式运作。除非我尝试调用deleteaStudent方法。如果我尝试从arrayList学生中删除学生,我会收到错误。

错误看起来像这样:

    Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
    at java.util.ArrayList$Itr.next(ArrayList.java:831)
    at studentapp.Studentapp.deleteaStudent(Studentapp.java:68)
    at studentapp.Studentapp.createMenu(Studentapp.java:115)
    at studentapp.Studentapp.main(Studentapp.java:28)
Java Result: 1

如何让它正常工作?

1 个答案:

答案 0 :(得分:0)

使用增强的for循环迭代时,您无法从列表中删除学生。您可以使用显式迭代器。

Iterator<Student> iter = students.iterator();
while (iter.hasNext() {
    Student student = iter.next();
    if (someCondition)
        iter.remove();
}

在你的方法中:

private static void deleteaStudent(int input){

    if (snumbers.contains(input)) {
        Iterator<Student> iter = students.iterator();
        while (iter.hasNext() {
            Student i = iter.next ();
            if(i.sNumber == input){
                iter.remove ();
                System.out.println(i.sNumber +" "+ i.firstName+" "+i.lastName + " has been deleted!");
            }
            else {
                System.out.println(" ");
            }
        }
    } else {
       System.out.println("Sorry, We cannot find a student with that number!");

    }
}