Okay so I have the following piece of code:
Random rnd = new Random();
int i = 0;
while(i<1000) {
String name = "event" + i;
Event name = new Event(rnd.nextInt(100000)); //ERROR duplicate variable
SimEngine.getScheduler().addEventToQueue(event);
i++;
}
System.out.println(SimEngine.getScheduler().getQueue().iterator());
I know that declaring name twice makes no sense, but I hope you can see what I’m trying to do. Because I want Event objects with names event1, event2 ,event3 etc.
How can I get it to let me use the String name as the name of the Event object?
Objects don’t (typically) have names. Variables have names. You really don’t want to have variables with names
event1,event2etc though.In this case, you should basically use an array, if you really need to have access to the events by index.
Of course, if you’re not going to use the variable afterwards, it’s pointless anyway, and you’d be just as well off with:
If this particular kind of class does have a name associated with each instance, you’d need to pass that name into the constructor, I suspect.