0

I have a program for Android with few classes. In my MainActivity i am using those classes. For example i have this class named Car. and so in my main activity i put this code to make a new "car" object.

Car car = new Car();

Now i also have a variable called count. that always increment by one. I want My main activity to create class every time (count%10 == 0). with a name like that car10,car20,car30. The number changer as the counter changes too. I did something like that and got stuck:

for(int i=0;i<100; i++)
{
if(i%10 == 0)
{
Car car+i = new Car();
}

}

but it didn't work. Any Idea?

Farhan Shah
  • 2,344
  • 7
  • 28
  • 54
HakoStan
  • 69
  • 2
  • 10

1 Answers1

1

In Java you cannot have variable variable names.

Consider using an array or a list. For example:

List<Car> list = new ArrayList<Car>();

// ...

list.add(new Car());

or if you need to name the indices, use a map:

Map<String, Car> map = new HashMap<String, Car>();

// ...

map.put("car" + i, new Car());
laalto
  • 150,114
  • 66
  • 286
  • 303
  • Hey, hmm i have another class called Stop. and his parameters are (Car car) how do i get the car class name in it? from the map. cuz i can't use string. i need to use a variable that his type is Car – HakoStan Apr 03 '14 at 13:00