[英]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.