java.util.ConcurrentModificationException&安培;迭代? [英] java.util.ConcurrentModificationException & iteration?
问题描述
我很很新的ArrayList&安培;迭代器和放大器;这是我第一次得到这个例外。
我有一个ArrayList U&安培;我想这样做下面的算法:
为(字符c:U){ 如果(K == 1){//基本情况 如果(isAnswer(S + u.get(0))) 的System.out.println(S + u.get(0)+是正确的序列。+的'\\ n');
返回;
} 其他{
u.remove(C);
puzzleSolve(K-1,S + C,U);
u.add(C);
removeLastChar(多个);
} } //对每个端
因为我搜索此异常一点点,我发现我无法删除iterms weth为每一个数组列表&安培;我需要使用迭代器
但我kinna弄糊涂了的地方和放大器;究竟我必须把一段时间(iter.hasNext())及这样的东西,这块code的。
我会感激之余,如果你能帮助我。
PS。 s是字符串(初始为空)及k是INT
的究竟如何,我必须把一段时间(iter.hasNext())的
块引用>您可以使用
迭代器
如下:的Iterator<性格> ITER = u.iterator();
而(iter.hasNext())
{
字符c = iter.next();
.....
}泛型初始化列表:
列表<性格> U =新的ArrayList<性格>();
提示:使用
iter.remove()
,iter.add()
只要适用,而不是u.remove()
和u.add()
。您需要从这里开始: http://www.tutorialspoint.com/java/java_using_iterator。 HTM
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
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
PS. s is String ( initially empty) & k is int
解决方案how exactly i must put the while(iter.hasNext())
You can use
iterator
as below:Iterator<Character> iter = u.iterator(); while(iter.hasNext()) { Character c = iter.next(); ..... }
Initialize your list with generics:
List<Character> u = new ArrayList<Character>();
Hint: use
iter.remove()
,iter.add()
wherever applicable instead ofu.remove()
andu.add()
.You need to start here: http://www.tutorialspoint.com/java/java_using_iterator.htm
这篇关于java.util.ConcurrentModificationException&安培;迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!