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?

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
  • 2
    Name your `Ball` variable something other than `i`, which is already used as the `for` loop index. – Sotirios Delimanolis Jan 07 '14 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). – Fallenreaper Jan 07 '14 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? – Sven van den Boogaart Jan 07 '14 at 20:09
  • What does it matter what the `Ball` variables are called? You can't dynamically name variables in Java. – Sotirios Delimanolis Jan 07 '14 at 20:09
  • if i know there are 5 balls i can call 4.dosomething – Sven van den Boogaart Jan 07 '14 at 20:09
  • @SvenB While testBall is only one, `myBalls.add(testBall)` puts them into `myBalls` one at a time. (ie: they add up) –  Jan 07 '14 at 20:10
  • No, since you have them in your `ArrayList`, you can instead do `myBalls.get(4).doSomething()`. – Sotirios Delimanolis Jan 07 '14 at 20:10
  • ok but these are diffrent object then because the name with wich ive added them is the same lets say ball ? – Sven van den Boogaart Jan 07 '14 at 20:11
  • @SvenB The variable name which points to the object has little to do with the actual `Ball` off hiding in memory. `myBalls.get(3)` and `testBall` could easily point to the same thing. –  Jan 07 '14 at 20:12
  • 1
    Read [this](http://stackoverflow.com/questions/6729605/dynamic-variable-names-in-java). An object does not have a name. A variable does. That name cannot be created dynamically. – Sotirios Delimanolis Jan 07 '14 at 20:13
  • First of all, I chuckled at the use of variable/class named 'balls'. Juvenile, I know. But, what the others are trying to say is that you can name the variable in your `for` loop anything you want. That name only exists in the "scope" it was created in. In your code, the `int i` exists in the scope of the `for` loop, and anything defined in the `for` loop exists only in the scope of that iteration. You do not have to name your variable "ball1", "ball2", etc. It seems like you are struggling with the concept of "scope", and I would suggest doing a little reading up. – CodeChimp Jan 07 '14 at 20:13
  • Ok clears alot, i was thinking it would be easyer to do call like ball1.jump() or something then to use byBalls.get(3).jump() but that isnt possible if i get it right? – Sven van den Boogaart Jan 07 '14 at 20:14
  • No, that is not possible. Use the `List`, array, or `Map` as described in the link, if you really want that. – Sotirios Delimanolis Jan 07 '14 at 20:17

2 Answers2

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);
  }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
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());
    }
}
danihodovic
  • 1,151
  • 3
  • 18
  • 28