2017-03-08 7 views
0

私は小さな学生の名簿を管理するプロジェクトに取り組んでいます。必要な機能の1つは、生徒IDを使用して生徒の記録を削除する機能と、生徒IDが存在しない場合にその削除機能がエラーを返す機能です。Java:配列リストの削除中にランタイムエラーが発生する

この削除検証に必要なテストは、同じ学生IDを2回削除し、2回目にremoveメソッドが呼び出されたときにエラーメッセージが表示されるかどうかを確認することです。

最初のremoveメソッド呼び出しが動作し、呼び出されたstudent IDの要素が正常に削除されます。ただし、2回目の削除では、Concurrent Modification Exceptionエラーが発生します。

次のコードは、Student配列リストを作成し、removeメソッドを(2回)呼び出します。

static ArrayList<Student> myRoster = new ArrayList<>(); 

public static void main(String[] args) 
{ 
    // add students to the roster 
    add("1", "John", "Smith", "[email protected]", "20", 
    88, 79, 59); 
    add("2", "Suzan", "Erickson", "[email protected]", "19", 
    91, 72, 85); 
    add("3", "Jack", "Napoli", "The_lawyer99yahoo.com", "19", 
    85, 84, 87); 
    add("4", "Erin", "Black", "[email protected]", "22", 
    91, 98, 82); 

    //loop through the ArrayList and for each element: 
    remove("3"); 
    remove("3"); 

ここに削除方法があります。ここで私はこれがどう動くべきか考えています:

*生徒IDが存在するかどうかを確認するために、if/elseを埋め込みif/elseループに入れます。

* containsを使用して、配列リストに学生IDが含まれているかどうかを確認します。そうであれば、if/elseが削除されます。そうでない場合は、失敗した印刷物を返して返します。ここで

public static void remove(String studentID) 
    //remove student from roster by student ID 
    //print error message if student is not found 
    { 
     for(Student b: myRoster) 
     { 
      String record = b.getStudentID(); 
      Boolean a = studentID.contains(studentID); 
      if (a) 
      { 
       Boolean c = record.matches(studentID); 
       if (c) 
       { 
        myRoster.remove(b); 
        System.out.println("Student ID " + studentID + " was removed."); 
       } 
      else 
       { 
        ; 
       } 
      } 
      else 
      { 
       System.out.println("Student ID does not exist."); 
      } 

私は取得していますエラーです:

java.util.ConcurrentModificationException 
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) 
at java.util.ArrayList$Itr.next(ArrayList.java:851) 
at Roster.remove(Roster.java:48) 
at Roster.main(Roster.java:28) 
java.util.ConcurrentModificationException 
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901) 
at java.util.ArrayList$Itr.next(ArrayList.java:851) 
at Roster.remove(Roster.java:48) 
at Roster.main(Roster.java:28) 

任意のアイデア?

+2

あなたは、foreachループで削除使用することはできません。通常のforループを使用します。 –

+0

[Javaプログラミングエラー:java.util.ConcurrentModificationException]の可能な複製(http://stackoverflow.com/questions/8553713/java-programming-error-java-util-concurrentmodificationexception) –

答えて

1

リストの内容をイテレータで反復処理している間は、安全に変更することはできません。これを行うにはListIteratorを使用してください。

ListIterator<Student> it = myRoster.listIterator(); 
while (it.hasNext()) { 
    Student b = it.next(); 
    if (b.getStudentId() == studentId) { 
     it.remove(); // Removes b from myRoster 
    } 
    ... 
} 

多くの生徒がいれば、これはうまく拡張できないことに注意してください。 Map<String, Student>であなたの名簿を保持する方が良いでしょう。鍵は学生IDです。

0

生徒IDが空白になっている最初のelse部分には存在しないとは言えませんか?

if (a) 
{ 
    Boolean c = record.matches(studentID); 
    if (c) 
    { 
     myRoster.remove(b); 
     System.out.println("Student ID " + studentID + " was removed."); 
    } 
    else 
    { 
     System.out.println("Student ID does not exist."); 
    } 
} 
0

これを試してみてください:

for(Iterator<String> it = myRoster.iterator(); it.hasNext();) { 
    String b = it.next(); 
    ... 
    if (...) { 
     it.remove(); 
     ... 
    } 
    ... 
} 
関連する問題