java.util.ConcurrentModificationException &迭代? [英] java.util.ConcurrentModificationException & iteration?
问题描述
我对 Arraylists & 非常陌生迭代器这是我第一次得到这个例外.我有一个 ArrayList u &我想做以下算法:
I'm so very new to Arraylists & iterators & this is the first time I got this exception. I have an ArrayList u & I'd like to do the following algorithm:
for (Character c:u){
if(k==1){ //base case
if(isAnswer(s+u.get(0)))
System.out.println(s+u.get(0)+" is the correct sequence."+ '\n');
return;
}
else{
u.remove(c);
puzzleSolve(k-1, s+c , u);
u.add(c);
removeLastChar(s);
}
} //end of for each
当我稍微搜索这个异常时,我发现我无法删除数组列表中的每个元素的 iterms &我需要使用迭代器但我很困惑在哪里&我必须如何准确地放置 while(iter.hasNext()) &这段代码的这些东西.如果你能帮助我,我将不胜感激
as I searched this exception a little bit i found out I can't remove iterms weth for each on a arraylist & I need to use iterator but i kinna got confused where & how exactly i must put the while(iter.hasNext()) & such stuff for this piece of code. i would be more than grateful if you could help me
附注.s 是字符串(最初为空)&k 是整数
PS. s is String ( initially empty) & k is int
推荐答案
我必须如何准确地放置 while(iter.hasNext())
您可以使用 iterator
如下:
Iterator<Character> iter = u.iterator();
while(iter.hasNext())
{
Character c = iter.next();
.....
}
用泛型初始化你的列表:List
Initialize your list with generics: List<Character> u = new ArrayList<Character>();
提示:在适用的地方使用 iter.remove()
、iter.add()
而不是 u.remove()
和 u.add()
.
Hint: use iter.remove()
, iter.add()
wherever applicable instead of u.remove()
and u.add()
.
您需要从这里开始:http://www.tutorialspoint.com/java/java_using_iterator.htm
这篇关于java.util.ConcurrentModificationException &迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!