1

I am trying to use a for() method to create a new variable each time with the variable. Like the following.

for(i = 1; i < 5; i++){
     // NEW VARIABLE //
}

the problem I am having is I am having is I am wanting to use i in the name of the variable. Like the following.

for(i = 1; i < 5; i++){
     Draw draw+i = new Draw();
}

How would I go about doing this? (I'm still new to java so no hate please)

user2892875
  • 99
  • 1
  • 1
  • 9
  • 6
    Why don't you use Arrays? http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Pradeep Simha Jan 02 '14 at 17:03
  • 3
    DON'T DO THIS. It's a common newbie idea but not a good idea. Use a Map; that will allow you to add values with meaningful names. – Jacob Mattison Jan 02 '14 at 17:03
  • As the others have stated, you should use an array or ArrayList for what you're trying to do -- to associate an object with a variable and an incrementally increasing number, but more importantly, you should know that variable names are not nearly as important in Java as you're making them out to be, and almost don't exist in compiled code. What matters are *references*, ways to get a handle on an object. Yes, this can be done via variable names, but also arrays, Lists, Maps, Trees and other collections. – Hovercraft Full Of Eels Jan 02 '14 at 17:08
  • @PradeepSimha You should make that an answer. –  Jan 02 '14 at 17:09
  • 1
    @dudeprgm: he can't and shouldn't. 1) the question is already and thankfully closed, and 2) this sort of question and answer has been given so many times on this site, that one more will not make the site any better. The OP instead needs to improve his searching skills, and with practice, he will. – Hovercraft Full Of Eels Jan 02 '14 at 17:10
  • I agree with @HovercraftFullOfEels – Pradeep Simha Jan 02 '14 at 17:11
  • @HovercraftFullOfEels Sorry, I added that comment before it was marked as a duplicate. –  Jan 02 '14 at 17:12
  • It is maybe worth noting that there are a few languages that allow this (REXX comes to mind) and it can be a useful (though dangerous) feature. But it's pretty impractical to do in a compiled language. – Hot Licks Jan 02 '14 at 17:24

0 Answers0