1

我是编码的完整初学者。我已经在这个论坛上寻找这个问题的解决方案,但没有找到它。

现在我一直在编写方法 removeFirstNote()。

每次我尝试编译时,都会收到一条错误消息:

java.util.-ConcurrentModificationException

这是我到目前为止所得到的......它需要通过 FOR-EACH 循环(学校任务)来完成。

public class Notebook
{
    private ArrayList<String> notes;

    public Notebook()
    {
        notes = new ArrayList<String>();
    }

    public void removeFirstNote(String certainString)
    {   int noteNumber = 0;
        boolean removed = false;

        for (String note : notes){

            if(note.contains(certainString) && !removed){
                notes.remove(noteNumber);
                removed = true;

            } else {

                noteNumber++;
            }
        }   
    }
4

5 回答 5

1

break在你第一次搬家后,

    public void removeFirstNote(String certainString)
    {   
        int noteNumber = 0;
        //boolean removed = false; //No need

        for (String note : notes)
        {    
            if(note.contains(certainString))
            {
                notes.remove(noteNumber);
                //removed = true;
                break;
            } 
            else 
            {    
                noteNumber++;
            }
        }   
    }

你会很想知道为什么ConcurrentModificationException
为此,您应该知道for-each循环是如何工作的。
for-eachList 的循环将在内部转换为带有迭代器的 for 循环。

for (Iterator<String> iterator = mylist.iterator(); iterator.hasNext();)

当您使用它然后从列表中删除一个元素时,构造的Iterator将不知道该更改的任何内容,并且会有一个ConcurrentModificationException.

于 2013-11-27T10:23:59.337 回答
1

你正面临ConcurrentModificationException ,因为你同时做两个操作list。即同时循环和删除。

为了避免这种情况,请使用迭代器,它可以保证您安全地从列表中删除元素。

 Iterator<String> it = notes.iterator();
        while (it.hasNext()) {
            if (condition) {
                it.remove();
                break;
            }
        }

如果没有迭代器,

1)你需要使用另一个列表

2)将所有元素添加到它。

3)在原始列表上循环

3)当条件满足时,remove从原始list

于 2013-11-27T10:22:14.517 回答
0

尝试:

for(String note : notes){
   if(!notes.contains(certainString){
      continue;
   }
   if(notes.contains(certainString)== true){
      System.out.println("String: " + certainString + " has been removed");
      notes.remove(certainString);
      break;
   }
   else{
     // do this
   }
于 2013-11-27T10:31:39.543 回答
0

您不能同时遍历列表并删除元素。如果您必须使用 for 循环,我建议您先遍历列表并标记适合的项目,然后再将其删除。

public void removeFirstNote(String certainString) {
  int noteNumber = 0;
  boolean removed = false;
  int index = -1;

  for (String note : notes) {

     if (note.contains(certainString) && !removed) {
        index = noteNumber;
        removed = true;
        //if your teacher allows you to, you can break the loop here and it all becomes easier
     } else {

        noteNumber++;
     }
  }
  if (index != -1) {
     notes.remove(index);
  }

}

于 2013-11-27T10:44:31.480 回答
0

您可以将其归结为以下内容:

public void removeFirstNote(String certainString) {
 for (String note: notes) {
  if (note.contains(certainString)) {
    notes.remove(note);
    break; // exit this loop.
  } 
 }
}

这更有效——一旦你找到了你要找的东西,就没有必要重复了。

希望这可以帮助。

文尼弗利特伍德

于 2013-11-27T10:30:32.010 回答