java.util.ConcurrentModificationException&安培;迭代? [英] java.util.ConcurrentModificationException & iteration?

查看:120
本文介绍了java.util.ConcurrentModificationException&安培;迭代?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很很新的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 of u.remove() and u.add().

You need to start here: http://www.tutorialspoint.com/java/java_using_iterator.htm

这篇关于java.util.ConcurrentModificationException&安培;迭代?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆