繁体   English   中英

ConcurrentModificationException使用迭代器循环时

[英]ConcurrentModificationException When using iterator loop

我正在使用迭代器,但在调用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);
    }
}

我的假设是我正在使用output.get(i)进一步访问该列表,但我认为在这种情况下我无法找到添加迭代器的可能方法。

基本上我有一个字符串列表,每个字符串都有测量的字体高度并将其添加到整数中。

您确定修改异常与迭代器有关吗? 该行有很多事情要做,还有其他可能出错的事情,与您的迭代器无关。

我建议将该行分成几行代码,并将iterator.next()的结果分配给一个变量。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM