ArrayList问题

时间:2010-06-08 19:53:44

标签: java arraylist

我应该在画布上创建一些随机球,这些球存储在ArrayList中(我希望这里的ArrayList是合适的:可供选择的替代选项是HashSet和HashMap)。 现在,无论我做什么,我都会在画布的顶部放置不同颜色的球,但是它们只是卡在那里而完全拒绝移动。 除此之外,我现在得到一个ConcurrentModificationException,在运行代码时:

java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at BallDemo.bounce(BallDemo.java:109)

第109行:bBall.draw();

读取该异常,我发现可以通过某种方式同步访问来确保以线程安全的方式访问ArrayList。但是,既然我记得同学们在没有同步的情况下做了什么,我的猜测是,这实际上是走错路。

也许你们可以帮助我让这个工作,我至少需要那些愚蠢的球来移动; - )

   /**
 * Simulate random bouncing balls
 */
public void bounce(int count)
{
    int ground = 400;   // position of the ground line
    System.out.println(count);

    myCanvas.setVisible(true);

    // draw the ground
    myCanvas.drawLine(50, ground, 550, ground);


    // Create an ArrayList of type BouncingBalls
    ArrayList<BouncingBall>balls =  new ArrayList<BouncingBall>();

    for (int i = 0; i < count; i++){
        Random numGen = new Random(); 
        // Creating a random color. 
        Color col = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
        // creating a random x-coordinate for the balls
        int ballXpos = numGen.nextInt(550);


        BouncingBall bBall = new BouncingBall(ballXpos, 80, 20, col, ground, myCanvas);

        // adding balls to the ArrayList
        balls.add(bBall);
        bBall.draw();
        boolean finished = false;
    }

        for (BouncingBall bBall : balls){

            bBall.move();

    }
}

这将是我们从老师那里得到的原始未经修改的方法,它只会产生两个球:

    /**
 * Simulate two bouncing balls
 */
public void bounce()
{
    int ground = 400;   // position of the ground line

    myCanvas.setVisible(true);

    myCanvas.drawLine(50, ground, 550, ground);
    // draw the ground

    // crate and show the balls
    BouncingBall ball = new BouncingBall(50, 50, 16, Color.blue, ground, myCanvas);
    ball.draw();
    BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
    ball2.draw();

    // make them bounce
    boolean finished =  false;
    while(!finished) {
        myCanvas.wait(50);           // small delay
        ball.move();
        ball2.move();
        // stop once ball has travelled a certain distance on x axis
        if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
            finished = true;
        }
    }
    ball.erase();
    ball2.erase();
}

}

所以我只修改了我的代码如下

public void bounce(int count)
    {
        int ground = 400;   // position of the ground line
        System.out.println(count);

        myCanvas.setVisible(true);

        // draw the ground
        myCanvas.drawLine(50, ground, 550, ground);


        // Create an ArrayList of type BouncingBalls
        ArrayList<BouncingBall>balls =  new ArrayList<BouncingBall>();
        Random numGen = new Random(); 
        for (int i = 0; i < count; i++){

            // Creating a random color. 
            Color col = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
            // creating a random x-coordinate for the balls
            int ballXpos = numGen.nextInt(550);


            BouncingBall bBall = new BouncingBall(ballXpos, 80, 20, col, ground, myCanvas);

            // adding balls to the ArrayList
            balls.add(bBall);
            bBall.draw();

        }
            boolean finished = false;
  while(!finished)
    {
        myCanvas.wait(50);           // small delay

        for(BouncingBall ball : balls)
        {
            ball.move();

            // once one ball has travelled the distance, they all have
            if(ball.getXPosition() >= 550)
                finished = true;
        }
    }

    for(BouncingBall ball : balls)
        ball.erase();
}

但是这只会很快移动球,然后会产生与上面相同的异常。

4 个答案:

答案 0 :(得分:3)

你错过了while(!finished)部分 如果您正在添加,那么您只能通过球进行一次迭代,这就是您没有看到它们移动的原因。

编辑:如果任意球(0到550之间)的随机X位置接近550,新版本可能很快就会结束。

答案 1 :(得分:0)

您是否被迫只使用这三种结构中的一种?一个ArrayList barfing-out ConcurrentModificationException几乎是一个刻有邀请,试图用CopyOnWriteArrayList替换它。

答案 2 :(得分:0)

public void bount(int count)
{
    int ground = 400;

    myCanvas.setVisible(true);

    myCanvas.drawLine(50, ground, 550, ground);

    ArrayList<BouncingBall> balls =  new ArrayList<BouncingBall>(); 

    for(int i = 0; i < count; i++)
    {
        // set up colors and position
        BouncingBall newBall = new BouncingBall(ballXpos, 80, 
            20, col, ground, myCanvas);

        newBall.Draw();

        balls.Add();
    }

    boolean finished = false;

    while(!finished)
    {
        myCanvas.wait(50);           // small delay

        for(BouncingBall ball : balls)
        {
            ball.Move();

            // once one ball has travelled the distance, they all have
            if(ball.getXPosition() >= 550)
                finished = true;
        }
    }

    for(BouncingBall ball : balls)
        ball.erase();
}

答案 3 :(得分:0)

您应该在for循环之前创建Random对象。 见下文。这可能会正确定位你的球

 Random numGen=new Random();

  for (int i = 0; i < count; i++){ 
           // Creating a random color.  
    Color col = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256)); 
    // creating a random x-coordinate for the balls 
    int ballXpos = numGen.nextInt(550); 


    BouncingBall bBall = new BouncingBall(ballXpos, 80