Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java Array

I am trying to create an array in a method to generate a number (int) amount of Balls specified by a user. However am getting a syntax error with this:

public void bounce(int numBalls)
 {

        //Create a collection of BouncingBalls call allBalls
        ArrayList<BouncingBalls> allBalls = new ArrayList<BouncingBalls>();

        // Loops through collection of allBalls to create balls
        for(BouncingBall ball: allBalls) {
            new BouncingBall(50, 50, 16, Color.blue, ground, myCanvas);
            ball.draw();
        }
}

5 Answers

If your class is BouncingBall, then your list should be of type BouncingBall not BouncingBall"s" as it is above.

I don't quite understand, on stack-overflow I came across

''' ArrayList<Class> myArray = new ArrayList<Class>(); '''

so for mine you're saying

''' BouncingBall myArray = new ArrayList<BouncingBall>(); '''

??

No.

In the first line underneath your comments, where you declare your allBalls ArrayList, you have to specifiy the type of the ArrayList by putting it in between brackets '<>' <--- look at what you actually put here.

Later in your enhanced for loop, you create an instance of your BouncingBall class by typing new BouncingBall.

In your ArrayList you are putting "BouncingBalls" between the brackets, but when creating an instance of your class you are just typing "BouncingBall"

The difference is one has a S on the end, and the other does not. I dont know which one is correct in your case, but they need to be the same. The type of your list, should match the proper class name for whatever it is you are trying to put in the list, the same as you would type it if you were creating an instance of it.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

If you cannot find the symbol for ArrayList, you need to import it. It is in the java.util package.

Thank you, make so much sense now. Is so obvious now I full understand what the error was saying.

The issue seems to be with the first ArrayList statement. Says 'cannot find symbol - class ArrayList' when I click compile.

BouncingBall is it's own separate class so it's object orientated.

Sorry i brainfarted, your syntax on the loop is fine. I ran it through something i am currently working on, and your basic naming convention caused no issue.