簡體   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