2014-04-22 48 views
0

我不能理解爲什麼我在底部的while循環中嵌入的for循環中獲得不兼容的類型。總是收到不兼容的類型錯誤時對其進行編譯,就上不兼容的類型 - 請幫助 - BlueJ BouncingBall演示

bounceBall = ball.get(ⅰ);任何人有什麼想法,我做錯了什麼?

import java.awt.Color; 
import java.util.ArrayList; 
import java.util.Random; 
public class BallDemo 
{ 
    private Canvas myCanvas; 
    private ArrayList ball; 
    private BouncingBall bounceBall; 

    private Random randomGenerator; 

    public BallDemo() 
    { 
     myCanvas = new Canvas("Ball Demo", 600,500); 
    } 

    public void bounce() 
    { 
     int ground = 400; 
     ball = new ArrayList(); 
     randomGenerator = new Random(); 
     myCanvas.setVisible(true); 
     myCanvas.erase(); 

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

     BouncingBall ball1 = new BouncingBall(position(), position(), 16, Color.BLUE, ground, myCanvas); 
     ball1.draw(); 
     BouncingBall ball2 = new BouncingBall(position(), position(), 20, Color.RED, ground, myCanvas); 
     ball2.draw(); 
     BouncingBall ball3 = new BouncingBall(position(), position(), 20, Color.BLACK, ground, myCanvas); 
     ball3.draw(); 

     ball.add(ball1); 
     ball.add(ball2); 
     ball.add(ball3); 

     boolean finished = false; 
     while (!finished) 
     { 
      myCanvas.wait(50); 

      for(int i = 0; i < ball.size(); i++) 
      { 
       bounceBall = ball.get(i); 
       bounceBall.move(); 
       if(bounceBall.getXPosition() >= 550) 
       { 
        finished = true; 
       } 
      } 
     } 
    } 
    public int position() 
    { 
     return randomGenerator.nextInt(200); 
    } 
} 

回答

0

我覺得需要這樣的:

private ArrayList<BouncingBall> ball;

那麼這樣的:

ball = new ArrayList<BouncingBall>();

+0

WOW,我知道這是什麼做的語法,我在幹什麼那麼錯了?或者可以請你澄清爲什麼我的語法關閉?你幫了我很多!在BlueJ中,它只是說我沒有輸入正確的類型......有點模糊。 – user3559061

+0

我應該將此推薦給那些更擅長解釋Java泛型與非泛型相關的人,因爲我是該語言的新手,並且我曾編程過的所有代碼都使用泛型(尖括號中包含的類型)。 – Don

+0

好吧,我必須再次感謝您的'通用'知識:D! – user3559061