主“java.util.ConcurrentModificationException”中的异常

时间:2014-12-17 04:16:30

标签: java arrays arraylist

像往常一样阵列和他们的名单将是我的死,只有这次实验室工作正常直到结束然后我得到一个强烈的错误,我作为一个java新手已经知道这是什么。希望你们中的一些更好的程序员能够帮助我。

错误+当前输出。

word with 2 vowels = 5
word with 3 vowels = 0
word with 4 vowels = 1
word with 2 chars = 0
word with 3 chars = 3
word with 4 chars = 2
word with 5 chars = 2
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at Words.removeWordsWithXChars(Words.java:54)
at Lab16c.main(Lab16c.java:25)

输出我想要

[one, two, three, four, five, six, seven, alligator]
word with 2 vowels = 5
word with 3 vowels = 0
word with 4 vowels = 1
word with 2 chars = 0
word with 3 chars = 3
word with 4 chars = 2
word with 5 chars = 2

after removing words with 3 chars 
[three, four, five, seven, alligator]

number of vowels in the words removed == 4

中的

代码

public class Lab16c
{
public static void main( String args[] )
{
    Words test = new Words("one two three four five six seven alligator");
    out.println(test);
    out.println("word with 2 vowels = "+test.countWordsWithXVowels(2));
    out.println("word with 3 vowels = "+test.countWordsWithXVowels(3));
    out.println("word with 4 vowels = "+test.countWordsWithXVowels(4));
    out.println("word with 2 chars = "+test.countWordsWithXChars(2));
    out.println("word with 3 chars = "+test.countWordsWithXChars(3));       
    out.println("word with 4 chars = "+test.countWordsWithXChars(4));
    out.println("word with 5 chars = "+test.countWordsWithXChars(5));
    int vowelsRemoved = test.removeWordsWithXChars(3);
    out.println("\nafter removing words with 3 chars \n" + test);
    out.println("\nnumber of vowels in the words removed == " + vowelsRemoved);
    out.println("\n\n");        


    //more test cases

}
}

第三类代码     public int countWordsWithXChars(int size)     {         int count = 0;         for(Word i:words)         {             if(i.getLength()== size)             {                 计数++;             }         }         返回计数;     }

//this method will remove all words with a specified size / length
//this method will also return the sum of the vowels in all words removed
public int removeWordsWithXChars(int size)
{
    for(Word i : words)
    {
        if(i.getLength() == size)
        {
            words.remove(i);
        }
    }
    return 0;
}

public int countWordsWithXVowels(int numVowels)
{
    int count=0;
    for(Word i: words)
    {
        if(i.getNumVowels() == numVowels)
        {
            count++;
        }
    }
    return count;
}
public String toString()
{
   return "";
}
}

希望你能找到我的问题,谢谢你的未来

2 个答案:

答案 0 :(得分:2)

使用集合的Iterater迭代元素,并使用迭代器删除元素。

public int removeWordsWithXChars(int size)
{

    Iterator<String> iterator = words.iterator();
    while(iterator.hasNext())
    {
        String word = iterator.next();
        if(word.length() == size)
        {
            iterator.remove(word);
        }
    }
    return 0;
}

希望这有帮助。

答案 1 :(得分:2)

您无法从java中的每个循环中删除列表中的项目。

考虑使用索引for循环,但是您需要跟踪已删除的已更改索引。

我建议您将要删除的项目存储在列表中。大致如此:

ArrayList<Word> wordsToRemove = new ArrayList<>();
for(Word word : words)
    wordsToRemove.add(word);
for(Word word : wordsToRemove)
    words.remove(words.indexOf(word));
wordsToRemove.clear();