I want create 10 objects in a loop and assign them to variables.
for (int i = 0; i <= 10; i++) {
Cat i = new Cat();
}
How can I create a variable name dynamically?
I want create 10 objects in a loop and assign them to variables.
for (int i = 0; i <= 10; i++) {
Cat i = new Cat();
}
How can I create a variable name dynamically?
Something like this might be the solution you are looking for,
ArrayList<Cat> catList = new ArrayList<>();
for(int idx = 0; idx < 10; idx++){
catList.add(new Cat());
}
// In order to retrieve items,
// catList.get(0) -> Object first inserted.
// catList.get(1) -> Object second inserted.....