Question: Variable name + integer

Hey! Im hoping that this is the right place to post my question, since im fairly new to this forum. Anyways, I was wondering if it was possible to make variables/instances with a loop in java or some other dynamical variable creation method. For example:
for(int i = 0; i < 10; i++)
{
Variable var+i = new Variable();
}

And in the end i would have:

var0, var1… var9

Well arrays are exactly wath you need,
heres a example code:

                 public class Arrayer {

                               private int number_Of_Numbers = 9;
                               private int[] number = new int[number_Of_Numbers];    // or just new int[9]

                                                 for (int i; i < number_Of_Numbers; i++){
                                                                   number[i] = i                                         // making the array number[0] = 0, number[1] = 1, etc
                                                     }
                                                                  //  so you can use them like
                                                                              number[3] = 5;
                                                                              number[2]++;
                                          }

But arrays always starts with cero, so int[] array = new int[5], will make 5 ints starting with array[0] and ending with array[4]
you can make any object array, you can make Button[] Butt = new Button[4], making 4 buttons, and in the init method a for loop is used to initialize them - for(int r; r < 4, r++){ Button[r] = new Button(“button”); Button[r].addActionListener(this); add(Button[r]); }

so your code will look like{

int[] var = new int[9];

   for(int i = 0; i < 9; i++)

{
var[i] = 1; // making all variables value 1
}
then you will have var[0], var[1], …, var[9];

  [b]Google for more Java arrays information [/b] :) 
                  i Hope this helped you

Array is not what i had in mind, since it is static. Ie: If i make array of 5, it will stay that way, even if the WWIII breaks out. However I need something that can change during the execution of application. Ie: User wants to add elements (ex: number of enemies) ->
not all users will set it to 200, so i would not want to waste resources on making an array of 200 elements.

Edit: Ty anyways for replying. :slight_smile:

Use ArrayList or some other collection: http://java.sun.com/javase/6/docs/api/java/util/package-summary.html

Ty! Im trying to use Vector, however there is a slight problem:

Vector vec = new Vector();
vec.add(0, new Foo(param, param, param));

Is there a way to use Foos methods ie: (vec.get(0)).update(); ?

Foo f = (Foo)v.get(0);

Ok that worked, ty. :slight_smile:

Preferably use generics for the Vector declaration.
e.g.


Vector<Foo> vec = new Vector<Foo>()

Vector is synchronized, so it’s (usually) slower than ArrayList. Don’t use Vector.