//This method compares two ArrayLists of strings and compares whether the words in one array list contain the letters in the other.
public static void remove()
{
//Create iterators for both stringList and letterList
Iterator<String> iterWords = stringList.iterator();
Iterator<String> iterLetters = letterList.iterator();
//First while loop will go over all strings in stringList via the iterWords iterator
while(iterWords.hasNext())
{
//iterWords now has a .next() call
String word = iterWords.next();
//Second while loop that should run over each letter in letterList and compare it to each word in stringList
while(iterLetters.hasNext())
{
//iterLetter now has a .next() call
String letter = iterLetters.next();
//if statement to remove the entry in stringList if it does not contain the current letter. It is this part that throws the illegalstateexception
if(word.contains(letter) == false)
{
//This is the line that is causing the illegalstateexceptions
iterWords.remove();
}
}
}
}
大家好,我正在寻求关于迭代两个ArrayList时遇到的异常的见解。我已经简化了上述的ArrayList并删除了与问题无关的任何方法。 在最后一个iterWords.remove()上我得到了一个illegalstateexception异常。在外部while循环中,我已经执行了iterWords.next(),所以iterWords.remove()应该可以看到要删除的东西。
我猜想这是因为我从内部while循环中调用iterWords.remove()而引起的异常。你认为这可能是原因吗? 感谢您能带来的任何见解。
remove
。 - Jim Garrison