How to create object names java
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 开发者_StackOverflow社区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
use a map .. use the string as the key and an object instance as the value.
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..
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());
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
Have a factory for object creation; within the factory you can have a naming strategy.
精彩评论