gpt4 book ai didi

Java如何在循环时添加到数组列表

转载 作者:行者123 更新时间:2023-12-01 08:03:21 24 4
gpt4 key购买 nike

package biz.boulter.state;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;

import biz.boulter.sword.Game;
import biz.boulter.sword.Particle;

public class Menu implements State{
private ArrayList<Particle> particles = new ArrayList<Particle>();
boolean inLoop = false;

public Menu(){

}

@Override
public void render(Graphics2D g) {
if(!inLoop){
for(Particle p: particles){
p.render(g);
}
}
}

@Override
public void tick() {
if(!inLoop){
for(Particle p: particles){
p.tick();
}
}
}

@Override
public void keyPressed(int kc) {

}

@Override
public void keyReleased(int kc) {

}

@Override
public void mousePressed(int button, int x, int y) {
for(int i = 0; i<500; i++){
int rand;
if(Game.rand.nextBoolean()){
rand = Game.rand.nextInt(10)-11;
}else{
rand = Game.rand.nextInt(10)+1;
}
particles.add(new Particle(x, y, rand, Game.rand.nextInt(10)-11, new Color(Game.rand.nextInt(1000000000))));
}
}

@Override
public void mouseReleased(int button, int x, int y) {

}
}

嘿伙计们,这是我的代码,我不断收到 ConcurrentModificationException。我知道这意味着有两件事同时改变粒子变量。但是我还应该如何添加到数组列表中。我看到一些论坛说使用迭代器,但那是为了删除而不是添加。

提前致谢!

编辑:

堆栈跟踪:

Exception in thread "Thread-2" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at biz.boulter.state.Menu.render(Menu.java:21)
at biz.boulter.sword.Game.render(Game.java:43)
at biz.boulter.sword.Game.run(Game.java:136)
at java.lang.Thread.run(Unknown Source)

粒子类别:

package biz.boulter.sword;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Particle {
private double x = 0;
private double y = 0;
private double xa = 0;
private double ya = 0;
private Color particleColour;
private Rectangle img;

public Particle(int x, int y, int xa, int ya, Color colour){
this.x = x;
this.y = y;
this.xa = xa;
this.ya = ya;
this.particleColour = colour;
img = new Rectangle(x, y, 5, 5);
}

public void render(Graphics2D g){
img.setBounds((int)Math.round(x), (int)Math.round(y), 5, 5);
g.setColor(particleColour);
g.fill(img);
}

public void tick(){
ya+=0.5;

if(xa < 0){
xa+=1;
}

if(xa > 0){
xa-=1;
}

x+=xa;
y+=ya;
}
}

最佳答案

您有 2 个选择。如果可以的话,将 tickrendermousePressed 方法标记为 synchronized。这将解决问题,但如果插入速度更快,这可能不是最佳解决方案。

另一个解决方案是改变

private ArrayList<Particle> particles = new ArrayList<Particle>(); 

private LinkedBlockingQueue<Particle> particles = new LinkedBlockingQueue<Particle>();

这是有效的,因为根据 documentation 迭代器方法的:

The returned iterator is a "weakly consistent" iterator that will never throw ConcurrentModificationException, and guarantees to traverse elements as they existed upon construction of the iterator, and may (but is not guaranteed to) reflect any modifications subsequent to construction.

关于Java如何在循环时添加到数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23469583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com