简体   繁体   English

ConcurrentModificationException使用迭代器循环时

[英]ConcurrentModificationException When using iterator loop

I'm using an iterator but I still seem to get the modification exception when calling iterator.next() 我正在使用迭代器,但在调用iterator.next()时似乎仍会收到修改异常

public void render(Graphics g) {
    if(consoleShown) {

        int boxHeight = 0;

        Iterator<String> iterator = output.iterator();
        while(iterator.hasNext()) {
            boxHeight += ((int)writefont.getStringBounds(iterator.next(), frc).getHeight());
        }

        if(boxHeight > 225) {
            boxHeight = 225;
        }

        for(int i = offset; i < offset + 25; i++) { //Offset being the line from the list being displayed.
            if(i < output.size()) {
                //Inverse the range to reverse the order of the list being displayed.
                //The irriterator adds the new value to the beginning of the list and not the end to avoid exceptions.
                g.drawString(output.get((int)(Utils.inverseRange(i, output.size())) - 1), 10, (i - offset + 1) * (int)writefont.getStringBounds(output.get(i), frc).getHeight());
            }
        }

        g.drawLine(0, boxHeight + 3, game.getWidth(), boxHeight + 3);
        g.drawString(input, 10, boxHeight + 12);
    }
}

My assumption is that I'm accessing the list using output.get(i) further down but I don't think there's a possible way I could add an iterator in that scenario. 我的假设是我正在使用output.get(i)进一步访问该列表,但我认为在这种情况下我无法找到添加迭代器的可能方法。

Basically I have a list of strings, each string has the font height measured and added to an integer. 基本上我有一个字符串列表,每个字符串都有测量的字体高度并将其添加到整数中。

Are you sure the modification exception is related to the iterator? 您确定修改异常与迭代器有关吗? There is a lot going on in that line, there are several other possible things that could be going wrong, not related to your iterator. 该行有很多事情要做,还有其他可能出错的事情,与您的迭代器无关。

I would recommend breaking that line up into several lines of code and assigning the results of iterator.next() to a variable. 我建议将该行分成几行代码,并将iterator.next()的结果分配给一个变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Iterator时出现ConcurrentModificationException - ConcurrentModificationException when using Iterator 使用iterator和iterator.remove()时出现ConcurrentModificationException - ConcurrentModificationException when using iterator and iterator.remove() 使用迭代器删除条目时出现 ConcurrentModificationException - ConcurrentModificationException when using iterator to remove entry 使用Iterator的ConcurrentModificationException - ConcurrentModificationException using Iterator ConcurrentModificationException 使用列表迭代器 java 删除元素时 - ConcurrentModificationException When removing element using list iterator java 使用迭代器时出现 java.util.ConcurrentModificationException 错误? - java.util.ConcurrentModificationException error when using iterator? ConcurrentModificationException尽管使用了Iterator并且已同步 - ConcurrentModificationException despite the using Iterator and synchronized For 循环与 Iterator 避免使用 ArrayList 的 ConcurrentModificationException - For loop vs. Iterator to avoid ConcurrentModificationException with an ArrayList Iterator.next()上的游戏循环上的ConcurrentModificationException - ConcurrentModificationException on Game Loop on Iterator.next() 与迭代器的ConcurrentModificationException - ConcurrentModificationException with iterator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM