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?

2

1 Answer 1

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());
Sign up to request clarification or add additional context in comments.

1 Comment

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

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.