Reputation: 47
Lets say I need to create lot of class objects for my main class so I was wondering if there was any way to create a loop that creates a new class object many times but each time it changes the name if the class object.
Hey I know you might be thinking why would you want to have multiple class objects that are the same for a program but I want to do something similar but I dont want to release my code for it. I will give a sample of the code if it is needed though
EDIT:I want my class classnamehere to take user input and every time they type something like "open that thing" It would create a new class object
Upvotes: 2
Views: 1033
Reputation: 3986
Have a factory for object creation; within the factory you can have a naming strategy.
Upvotes: 0
Reputation: 18509
You can take a ArrayList like this:
List<YourClassType> list=new ArrayList<YourClassType>();
the put a for loop for how many objects you wanna create:
for(int i=0;i<100;i++)
list.add(new YourClassType());
Upvotes: 1
Reputation: 5921
If the requirement is to create set of objects of the same class and identify them uniquely - best thing is to create an array and initialize each via a loop...
Thanks..
Upvotes: 1
Reputation: 503
use a map .. use the string as the key and an object instance as the value.
Upvotes: 1
Reputation: 5321
Some code will be of great help, but can you not use an array of your class and create objects in a loop?
for example if there is a class named Example.
Example[] e = new Example[200];
for(int i=0;i<200;i++){
e[i]=new Example();
}
You can use a List too if you want more objects to be added dynamically. Thanks
Upvotes: 0