1

I have a Class with Balls.

In another class called CollectionBalls i have a collection of Balls (what a suprise)

The class CollectionBalls has a ArrayList of the type balls :

 ArrayList <Balls> myBalls;

What i want to do is when i create the object CollectionBalls is : set the ammount of balls in the parameter of the constructor.

like

public CollectionBalls(int amountOfBalls)
{
    myBalls = new ArrayList <Balls>();
    setAmountOfBalls(amountOfBalls);
}

public void setAmountOfBalls(int amountOBalls)
{
    for (int i = 0; i < amountOBalls;  i ++)
    {
        // Create a new ball
        Ball i = new Ball();
        // Add the ball to the collection of ball
        myBalls.add(i);
    }
}

but i cant create a new ball dynamicly with i.

How can i create the amount of object based on the parameter?

edit: i can rename i with something like testBall but testBall is one object then instead of 10 object like ball 1 ball 2 ball 3 right?

13
  • 2
    Name your Ball variable something other than i, which is already used as the for loop index. Commented Jan 7, 2014 at 20:06
  • the constructor for Ball must also not have any arguments or you would need to supply them (Overloading constructors is also a thing). Commented Jan 7, 2014 at 20:07
  • @SotiriosDelimanolis i can rename i with something like testBall but testBall is one object then instead of 10 object like ball 1 ball 2 ball 3 right? Commented Jan 7, 2014 at 20:09
  • What does it matter what the Ball variables are called? You can't dynamically name variables in Java. Commented Jan 7, 2014 at 20:09
  • 1
    Read this. An object does not have a name. A variable does. That name cannot be created dynamically. Commented Jan 7, 2014 at 20:13

2 Answers 2

2

I see two issues

public void setAmountOfBalls(int amountOBalls)
{
  for (int i = 0; i < amountOBalls;  i++)
  {
    // Create a new ball
    // Ball i = new Ball(); // you have a plural class name, but were trying to 
                            // make it singular here and you already have an 
                            // integer variable called i.
    Balls ball = new Balls();
    // Add the ball to the collection of ball
    myBalls.add(ball);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thx i used balls for the example to make it easyer to understand have adjusted it
it was possible with char name = 'a' name ++;
2
public CollectionBalls(int amountOfBalls)
{
    myBalls = new ArrayList <Balls>();
    setAmountOfBalls(amountOfBalls);
}

public void setAmountOfBalls(int amountOBalls)
{
    for (int i = 0; i < amountOf**Registers**;  i ++)

        // Add the ball to the collection of ball
        myBalls.add(new Balls());
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.